码迷,mamicode.com
首页 > 编程语言 > 详细

137. Single Number II java solutions

时间:2016-06-23 12:54:27      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

Given an array of integers, every element appears three times except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

 

Subscribe to see which companies asked this question

 
 1 public class Solution {
 2     public int singleNumber(int[] nums) {
 3         int[] bitcnt = new int[32];
 4         int ans = 0;
 5         for(int i = 0; i< 32;i++){
 6             for(int n : nums){
 7                 if(((n >> i) & 1) == 1){
 8                     bitcnt[i]++;
 9                 }
10             }
11             ans |= (bitcnt[i]%3) << i;
12         }
13         return ans;
14     }
15 }

平常位运算 使用的太少了,本题可以用map解决,也可以使用异或运算解决。参见其他大牛的帖子:

http://www.cnblogs.com/springfor/p/3870863.html

 

http://blog.csdn.net/derrantcm/article/details/47745445

137. Single Number II java solutions

标签:

原文地址:http://www.cnblogs.com/guoguolan/p/5610195.html

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