[LeetCode]Single Number II

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) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
		int one=0;
		int two=0;
		int three=0;
		for(int i=0;i<n;i++)
		{
			two|=(one&A[i]);
			one^=A[i];
			three=~(one&two);
			one&=three;
			two&=three;
		}
		return one;
    }
};

  

posted @ 2013-11-17 12:31  七年之后  阅读(138)  评论(0编辑  收藏  举报