Given an array of integers, every element appears three times except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
int singleNumber(vector<int>nums)
{
size_t num = 0;
int res = 0;
for(int i=0; i<32; ++i)
{
for(int j=0; j<nums.size(); ++j)
{
num += (nums[j]>>i) & 1;
}
res = res | (num % 3) << i;
num = 0;
}
return res;
}
};
int main(int argc, char const *argv[])
{
int a[] = {3,4,4,4};
vector<int>nums(a,a+4);
Solution S;
cout<<S.singleNumber(nums)<<endl;
return 0;
}