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

Leetcode# 137 Single Number II

时间:2015-01-25 18:08:31      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

原题地址

 

遍历所有数字,统计每一位出现的次数,模3即为只出现一次的数字在那一位的情况。

 

代码:

 1 int singleNumber(int A[], int n) {
 2   int count[32] = {0};
 3 
 4   for (int i = 0; i < n; i++) {
 5     for (int j = 0; j < 32; j++) {
 6       count[j] += A[i] & 1;
 7       A[i] >>= 1;
 8     }
 9   }
10 
11   int res = 0;
12   for (int j = 0; j < 32; j++) {
13     res += (count[j] % 3) << j;
14   }
15 
16   return res;
17 }

 

Leetcode# 137 Single Number II

标签:

原文地址:http://www.cnblogs.com/boring09/p/4248440.html

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