-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path56_2_Number_Once.cpp
More file actions
73 lines (54 loc) · 1.67 KB
/
56_2_Number_Once.cpp
File metadata and controls
73 lines (54 loc) · 1.67 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// Created by mark on 2019/7/25.
// Copyright © 2019年 mark. All rights reserved.
//
/*
说明:
1. 问题:56_2. 数组中有的出现三次,其中只有一个出现一次,找出这一个。
2. 思路:不能直接异或了。如果一个数字出现三次,则二进制的每一位也出现三次。
所以,把数组中所有数字二进制表示的每一位都加起来,某一位的和能被3整除,则出现一次的该位为0,不能整除该位为1;
*/
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <assert.h>
#include <cstdio>
#include <fstream>
#include <map>
#include <set>
using namespace std;
// 设置一个32位数组,存放所有元素每位为1的累加和
int FindNumberAppearOnce(vector<int> data)
{
if(data.size() == 0)
return NULL;
vector<int> bitSum{0}; // 记录所有元素某一位为1的个数和
for(int i = 0; i < data.size(); ++i)
{
int mask = 1;
for(int j = 31; j >= 0; j--) // 遍历当前数字的所有32位
{
int bit = data[i] & mask; // 看当前数字某一位是否为1
if(bit != 0)
bitSum[j]++;
mask = mask << 1; // 判断下一位
}
}
int res = 0;
for(int i = 0; i < 32; ++i)
{
res = res << 1;
res += bitSum[i] % 3; // 得到res第i位是否为1
}
return res;
}
int main()
{
vector<int> data{2,1,2,1,1,2,5};
int res = FindNumberAppearOnce(data);
cout << "一个的数字是:" << res << endl;
return 0;
}