查找数组中只出现一次的数

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 result = 0;
	    int size = 8*sizeof(int);
	    int factor = 1;
	    for (int i = 0; i < size; i++)
	    {
		    int count = 0;
		    for (int j = 0; j < n; j++)
		    {
			    if (A[j] & factor)
			    {
				    count++;
			    }
		    }
		    if (count % 3)
		    {
			    result += factor;
		    }
		
		    factor <<= 1;
	    }

	    return result; 
    }
};


你可能感兴趣的:(算法)