`
blue2048
  • 浏览: 182621 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

[leetcode]Single Number II - java

阅读更多
分别存储每一个bit位1出现的次数,然后对3取模
public class Solution {
    public int singleNumber(int[] A) {
         int[] bitNum = new int[32];
        int result =0;
        for (int j = 0; j < 32; j++) {
            for (int i = 0; i < A.length; i++) {
                int ele = A[i];
                int ifExist = ele & (1 << j);
                if (ifExist != 0) {
                    bitNum[j] = ++bitNum[j];
                }
            }
            bitNum[j] = bitNum[j]%3;
            if(bitNum[j] != 0){
                result |= bitNum[j]<<j;
            }
        }
        return result;    
    }
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics