LeetCode 137. Single Number II(单个数字)

33 篇文章 0 订阅
16 篇文章 0 订阅

原题网址:https://leetcode.com/problems/single-number-ii/

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?

方法一:使用哈希映射进行计数。

public class Solution {
    public int singleNumber(int[] nums) {
        Map<Integer, Integer> counts = new HashMap<>();
        Set<Integer> single = new HashSet<>();
        for(int i=0; i<nums.length; i++) {
            Integer count = counts.get(nums[i]);
            if (count == null) {
                single.add(nums[i]);
                counts.put(nums[i], 1);
            } else if (count == 1) {
                counts.put(nums[i], count + 1);
            } else {
                single.remove(nums[i]);
                counts.remove(nums[i]);
            }
        }
        if (single.size() == 1) return single.iterator().next();
        else return -1;
    }
}

方法二:对各位的1比特进行计数。

public class Solution {
    public int singleNumber(int[] nums) {
        int[] counts = new int[32];
        for(int num: nums) {
            int mask = 1;
            for(int i=0; i<32; i++) {
                if ((num & mask) != 0) {
                    counts[i] ++;
                }
                mask <<=1;
            }
        }
        int number = 0;
        int mask = 1;
        for(int i=0; i<32; i++) {
            if (counts[i] != 0 && (counts[i] % 3) != 0) {
                number |= mask;
            }
            mask <<=1;
        }
        return number;
    }
}

方法三:人工实现3进制加法。

public class Solution {
/*
http://www.acmerblog.com/leetcode-single-number-ii-5394.html
http://blog.csdn.net/kenden23/article/details/13625297
http://blog.csdn.net/foreverling/article/details/49718429
http://www.cnblogs.com/daijinqiao/p/3352893.html
*/
public int singleNumber(int[] A) {
    int ones = 0, twos = 0;
    for(int i = 0; i < A.length; i++){
        ones = (ones ^ A[i]) & ~twos;
        twos = (twos ^ A[i]) & ~ones;
    }
    return ones;
}
}

另一种实现:

public class Solution {
/*
http://www.acmerblog.com/leetcode-single-number-ii-5394.html
http://blog.csdn.net/kenden23/article/details/13625297
http://blog.csdn.net/foreverling/article/details/49718429
http://www.cnblogs.com/daijinqiao/p/3352893.html
*/
    public int singleNumber(int[] A) {
        int bit1 = 0, bit0 = 0;
        for(int i=0; i<A.length; i++) {
            int bit1n = bit0 & A[i];
            int bit0n = bit0 ^ A[i];
            int bit1nn = bit1 ^ bit1n;
            int three = ~(bit1nn & bit0n);
            bit1 = bit1nn & three;
            bit0 = bit0n & three;
        }
        return bit0;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值