-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsingle-number-iii.cpp
47 lines (35 loc) · 1.02 KB
/
single-number-iii.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
long long twoNumbers = 0;
for(int i: nums) {
twoNumbers ^= i;
}
long long lastSetBit = twoNumbers & -twoNumbers;
int first = 0;
int second = 0;
for(int i: nums) {
if ( (long long) i & lastSetBit) {
first^=i;
} else {
second^=i;
}
}
return { first, second };
}
};
// Set based approach
// vector<int> singleNumber(vector<int>& nums) {
// vector<int> result;
// map<int, int> occurenceCount;
// for( int i = 0; i < nums.size(); i++) {
// occurenceCount[nums[i]] += 1;
// }
// a
// for( auto occurencea: occurenceCount) {
// if (occurence.second == 1) {
// result.push_back(occurence.first);
// }
// }
// return result;
// }