-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_number.cpp
47 lines (39 loc) · 952 Bytes
/
single_number.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/
// June 15, 2016
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int singleNumber(vector<int>& nums)
{
int xor_val = nums[0];
for (vector<int>::iterator it = nums.begin()+1; it != nums.end(); ++it)
{
xor_val ^= *it;
}
return xor_val;
}
};
void test_xor(int num1, int num2)
{
int num3 = num1^num2;
cout << "xor: " << num3 << endl;
}
int main(int argc, char* argv[])
{
/*
int num1 = -2;
int num2 = 4;
test_xor(num1, num2);
*/
vector<int> nums = {-1,-3,2,-1,2};
Solution sln;
int single_number = sln.singleNumber(nums);
cout << "single number: " << single_number << endl;
return 0;
}
/**
* http://programmers.stackexchange.com/questions/239036/how-are-negative-signed-values-stored#
* http://stackoverflow.com/questions/19067286/xor-operation-in-c
*/