[leedcode 137] Single Number II

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

public class Solution {
    public int singleNumber(int[] nums) {
        //int的32个bit逐个处理,每一个bit进行相加,然后mod3,即可判断多余的一位是1或0.此思想同样适用于均出现三次,一个出现两次场景
        int res=0;
        for(int i=0;i<32;i++){
            int count=0;
            for(int j=0;j<nums.length;j++){
                count+=(nums[j]>>i)&1;
            }
           // res+=(count%3)<<i;//均可
            res|=(count%3)<<i;
        }
        return res;
    }
    //http://blog.csdn.net/kenden23/article/details/13625297
}

 

posted @ 2015-07-26 18:50  ~每天进步一点点~  阅读(154)  评论(0编辑  收藏  举报