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 one=0;
int two=0;
int three=0;
for(int i=0; i<n; i++){
two= (one & A[i]) | two;
one= one ^ A[i];
three = one & two;
// three=1表示1已经出现了3此,two和one需要清空
two &= ~three;
one &= ~three;
}
return one;
}
};leetCode: Single Number II [137],布布扣,bubuko.com
leetCode: Single Number II [137]
原文地址:http://blog.csdn.net/harryhuang1990/article/details/35595735