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

[LeetCode]137 Single Number II

时间:2015-01-08 18:15:42      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:leetcode

https://oj.leetcode.com/problems/single-number-ii/

http://blog.csdn.net/linhuanmars/article/details/22645599

public class Solution {
    public int singleNumber(int[] A) {
        return singleNumber_BitCompare(A, 3);
    }
        
    private int singleNumber_BitCompare(int[] A, int k)
    {
        int toReturn = 0;
        
        for (int i = 0 ; i < 32 ; i ++)
        {
            int tocompare = 1 << i;
            int occur = 0;
            for (int num : A)
            {
                if ((num & tocompare) != 0) // use != 0 rather than ==1
                    occur ++;
            }
            
            if (occur % k == 1) // normally it would be 0
                toReturn |= tocompare;
        }
        return toReturn;
    }
}


[LeetCode]137 Single Number II

标签:leetcode

原文地址:http://7371901.blog.51cto.com/7361901/1600765

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