代码改变世界

leetcode - Single Number II

2013-10-14 10:34  张汉生  阅读(152)  评论(0编辑  收藏  举报

  

 1 class Solution {
 2 public:
 3     int singleNumber(int A[], int n) {
 4         // Note: The Solution object is instantiated only once and is reused by each test case.
 5         if (n<=0)
 6             return 0;
 7         int bits[32];
 8         int i, j;
 9         for (i=0; i<32; i++)
10             bits[i]=0;
11         for (i=0; i<n; i++)
12             for (j=0; j<32; j++)
13                 bits[j] = ( bits[j] + ((A[i]>>j)&1) ) % 3;
14         int ans=0;
15         for (i=0; i<32; i++)
16             ans = ans+(bits[i]<<i);
17         return ans;
18     }
19 };