-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path260.cpp
40 lines (36 loc) · 916 Bytes
/
260.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
//
// 260.cpp
// leetcode
//
// Created by R Z on 2018/3/14.
// Copyright © 2018年 R Z. All rights reserved.
//
#include <stdio.h>
#include <vector>
#include <functional>
#include <numeric>
using namespace std;
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
// Pass 1 :
// Get the XOR of the two numbers we need to find
int diff = accumulate(nums.begin(), nums.end(), 0, bit_xor<int>());
// Get its last set bit
diff &= -diff;
// Pass 2 :
vector<int> rets = {0, 0}; // this vector stores the two numbers we will return
for (int num : nums)
{
if ((num & diff) == 0) // the bit is not set
{
rets[0] ^= num;
}
else // the bit is set
{
rets[1] ^= num;
}
}
return rets;
}
};