LeetCode--Single Number II

思路:

统计每位出现的次数,mod3;对于k同样适用

 1 class Solution {
 2 public:
 3     int singleNumber(int A[], int n) {
 4         int a[32] = {0};
 5         int i = 0;
 6         while(i < n)
 7         {
 8             int j = 0;
 9             int num = A[i];
10             while(j < 32)
11             {
12                 a[j] += num&1;
13                 num = num>>1;
14                 ++j;
15             }
16             ++i;
17         }
18         i = 0;
19         int ans = 0;
20         while(i < 32)
21         {
22             if(a[i]%3 != 0)
23             {
24                 ans = ans|(1<<i);
25             }
26             ++i;
27         }
28         return ans;
29     }
30 };
原文地址:https://www.cnblogs.com/cane/p/3880408.html