Leetcode: 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?


思路和Single Number 类似


思路1:先排序,后寻找是否三个连续值相同,不同则return这个值。用到Arrays.sort函数,时间复杂度不符合要求。


Solution1:

public class Solution {
    public int singleNumber(int[] A) {
        if (A == null || A.length == 0) {
            return -1;
        }
        if (A.length < 3) {
            return A[0];
        }
        
        Arrays.sort(A);
        for (int i = 0; i < A.length; i++) {
            if (i + 2 >= A.length - 1 || A[i] != A[i + 2]) {
                return A[i];
            }
            i += 2;
        }
        
        return -1;
    }
}


思路2:使用三进制异或。存三个变量,ones存出现过一次的数,twos存出现过两次的数,threes则判断这个数是否同时在ones和twos里面出现,若是则说明该数出现了三次,这时需要将这个数从ones和twos中删除。最后的出来的ones就是结果。


Solution2:

public class Solution {
    public int singleNumber(int[] A) {
        if (A == null || A.length == 0) {
            return -1;
        }
        
        int ones = 0, twos = 0, threes = 0;
        for (int i = 0; i < A.length; i++) {
        	twos |= ones & A[i];
        	ones ^= A[i];
        	threes = ones & twos;
        	ones &= ~threes;
        	twos &= ~threes;
        }
        
        return ones;
    }
}

 思路3:模拟三进制异或。即:各个数的某个二进制位之和能被3整除,则说明要寻找的single number在这一位上位0,反之为1。


Solution3:

public int singleNumber(int[] A) {
        if (A == null || A.length == 0) {
            return -1;
        }
        
        int res = 0;
        int[] bit = new int[32];
        for (int i = 0; i < 32; i++) {
        	for (int j = 0; j < A.length; j++) {
        		bit[i] += A[j] >> i & 1;
        		bit[i] %= 3;
        	}
        	res |= bit[i] << i;
        }
        
        return res;
    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值