1.描述
Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
2.分析
想了很久没想出比较好的办法,参考了下面的解题技巧:
http://www.cnblogs.com/grandyang/p/4263927.html
3.代码
int singleNumber(int* nums, int numsSize) {
if (NULL == nums || numsSize <= 0) exit(-1);
int a = 0, b = 0;
for (unsigned int i = 0; i < numsSize; ++i) {
b = (b ^ nums[i]) & ~a;
a = (a ^ nums[i]) & ~b;
}
return b;
}