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

lintcode-medium-Single Number II

时间:2016-04-06 07:02:44      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

Given 3*n + 1 numbers, every numbers occurs triple times except one, find it.

 

Example

Given [1,1,2,3,3,3,2,2,4,1] return 4

Challenge

One-pass, constant extra space.

 

public class Solution {
    /**
     * @param A : An integer array
     * @return : An integer 
     */
    public int singleNumberII(int[] A) {
        // write your code here
        
        if(A == null || A.length == 0)
            return 0;
        
        int[] count = new int[32];
        
        for(int i = 0; i < 32; i++){
            for(int j = 0; j < A.length; j++){
                count[i] += (A[j] >> i) & 1;
                count[i] %= 3;
            }
        }
        
        int res = 0;
        for(int i = 0; i < 32; i++)
            res = res | (count[i] << i);
        
        return res;
    }
}

 

lintcode-medium-Single Number II

标签:

原文地址:http://www.cnblogs.com/goblinengineer/p/5357668.html

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