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?
class Solution {
public:
int singleNumber(int A[], int n) {
int bitcount=sizeof(int) * 8;
int record[bitcount];
memset(record,0,bitcount*sizeof(int));
int mask;
for(int i=0;i<n;++i)
for(int j=0;j<bitcount;++j)
{
record[j]+=A[i]>>j&1;
}
int result=0;
for(int i=0;i<bitcount;++i)
result|=(record[i]%3)<<i;
return result;
}
};
模拟三进制