-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_number2.cpp
56 lines (50 loc) · 1.42 KB
/
single_number2.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
48
49
50
51
52
53
54
55
56
// =====================================================================================
//
// Filename: single_number2.cpp
//
// Description: 137. Single Number II. Given a non-empty array of integers, every
// element appears three times except for one, which appears exactly
// once. Find that single one.
//
// Version: 1.0
// Created: 08/31/2019 10:47:47 AM
// Revision: none
// Compiler: g++
//
// Author: Zhu Xianfeng (), [email protected]
// Organization:
//
// =====================================================================================
#include <stdio.h>
#include <vector>
using std::vector;
class Solution
{
public:
int singleNumber(vector<int>& nums)
{
// a a a b
// 1 1 1 1 -> (1 + 1 + 1 + 1) % 3 = 1
// 1 1 1 0 -> (1 + 1 + 1 + 0) % 3 = 0
// 0 0 0 1 -> (0 + 0 + 0 + 1) % 3 = 1
// 0 0 0 0 -> (0 + 0 + 0 + 0) % 3 = 0
int single = 0;
for (int i = 0; i < 32; i++)
{
int sum = 0;
for (auto n: nums)
{
sum += (n >> i) & 0x1;
}
single |= (sum % 3) << i;
}
return single;
}
};
int main(int argc, char* argv[])
{
vector<int> nums = {0, 1, 0, 1, 0, 1, 99};
int single = Solution().singleNumber(nums);
printf("The single number: %d\n", single);
return 0;
}