您的位置:首页 > 其它

LeetCode题解:Single Number II

2016-02-09 17:30 363 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: