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?
题意:给定整数数组,除了某个数,其余数都出现3次。找出那个数。要求O(n)时间,O(1)空间
思路:看不懂……据说通过电路分析和卡诺图可以得到式子
代码:
public int singleNumber(int[] A) {
int ones = 0, twos = 0;
for(int i = 0; i < A.length; i++){
ones = (ones ^ A[i]) & ~twos;
twos = (twos ^ A[i]) & ~ones;
}
return ones;
}