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?
具体分析参见位运算之找出奇特的数
直接上代码
public int singleNumber(int[] A) {
int[] digit = new int[32];
int result = 0;
for (int i = 0; i < 32; i++) {
for (int j = 0; j < A.length; j++) {
digit[i] += (A[j] >> i) & 1;
}
}
for (int i = 0; i < 32; i++)
result |= (digit[i] % 3) << i;
return result;
}