Skip to content

Commit 2e4442e

Browse files
committed
Single Number III: xor, try to partition all numbers into two groups.
1 parent d6a7ac3 commit 2e4442e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Medium/260_Single_Number_III.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "../Header.h"
2+
3+
using namespace std;
4+
vector<int> singleNumber(vector<int>& nums) {
5+
int xory = 0, pos = 1, x = 0, y = 0;
6+
for (int i = 0; i < nums.size(); i++) xory ^= nums[i];
7+
while (xory) {
8+
if (xory & 1) break;
9+
xory >>= 1;
10+
pos <<= 1;
11+
}
12+
for (int i = 0; i < nums.size(); i++) {
13+
if (nums[i] & pos) x ^= nums[i];
14+
else y ^= nums[i];
15+
}
16+
return vector<int>{x, y}
17+
}
18+

0 commit comments

Comments
 (0)