码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode--Single Number II

时间:2014-07-31 13:14:46      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   div   amp   leetcode   log   

思路:

统计每位出现的次数,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 };

 

LeetCode--Single Number II,布布扣,bubuko.com

LeetCode--Single Number II

标签:style   blog   color   io   div   amp   leetcode   log   

原文地址:http://www.cnblogs.com/cane/p/3880408.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!