Single Number II
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?
int singleNumber(int A[], int n) {
int result = 0;
int INT_SIZE = sizeof(int)*8;
int* count = new int[INT_SIZE];
memset(count,0,INT_SIZE*sizeof(int));
for(int i = 0;i<n;i++)
{
for(int j = 0;j<INT_SIZE;j++)
count[j] += (A[i]>>j)&1;
}
for(int i = 0; i<INT_SIZE;i++)
{
if(count[i] % 3 != 0)
result |= 1<<i;
}
delete [] count;
return result;
}
已AC