首页 > 其他 > 详细

LeetCode--Single Number II

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

思路:

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!