-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_260_SingleNumber_III.cpp
47 lines (38 loc) · 1.36 KB
/
LC_260_SingleNumber_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
/*
https://leetcode.com/problems/single-number-iii/
260. Single Number III
Given an integer array nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order.
You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.
*/
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
/******* using xor **************/
// long long xor_all=0;
int xor_all=0;
for(int x: nums)
xor_all ^= x; //now xor contains only xor of two odd occuring numbers, and duplicates are removed.
// get the rightmost set bit
// int right_set_bit = xor_all & (-xor_all);
int right_set_bit = 1;
while ((xor_all & right_set_bit) == 0) right_set_bit <<= 1;
int num1=0, num2=0;
for(int x: nums){
if(right_set_bit & x)
num1 ^= x;
else
num2 ^= x;
}
return {num1, num2};
/******* using set **************/
// set<int> s;
// for(int x:nums)
// {
// if(s.count(x))
// s.erase(x);
// else
// s.insert(x);
// }
// return vector<int>(s.begin(),s.end());
}
};