问题描述:序列中除了一个数只出现一次其余数均出现三次 求这个数
解决方案:不断求各个位的和然后模3最后所得的数就是只出现一次的树,时间复杂度为32*n
#include<iostream>
#include<vector>
using namespace std;
class Solution
{
public:
int singNum(vector<int>&data)
{
int count = 32;
int ret = 0;
int mul = 1;
for (int i = 1; i <= count; i++)
{
int add = 0;
for (int j = 0; j < data.size(); j++)
{
add += (data[j] >> (i - 1)) & 0x0001;
}
ret += (add % 3)*mul;
mul = mul * 2;
}
return ret;
}
};
int main()
{
Solution s;
vector<int> d = {1,2,1,2,1,2,7};
cout << s.singNum(d) << endl;
system("pause");
}