-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path136-Single-Number.cpp
More file actions
35 lines (27 loc) · 908 Bytes
/
136-Single-Number.cpp
File metadata and controls
35 lines (27 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public:
int singleNumber(vector<int>& nums) {
if(nums.size() > 1){
sort(nums.begin(), nums.end());
for(int i=0; i<nums.size(); i++){
// Check if it is not a pair
if(nums[i] != nums[i+1]){
return nums[i];
}
// Jump to next pair
i++;
}
}
return nums[0];
}
};
/* 136. Single-Number.cpp
//////////////////////////////////////////////////
Given a non-empty array of integers nums, every element appears twice except for one.
Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
Input: nums = [4,1,2,1,2]
Output: 4
https://leetcode.com/problems/single-number/
//////////////////////////////////////////////////
*/