首页 > 编程语言 > 详细

137. Single Number II java solutions

时间:2016-06-23 12:52:13      阅读:141      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!