LeetCode-Single NumberI II III

61 篇文章 10 订阅
37 篇文章 0 订阅

I)

Given an array of integers, every element appears twice 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 int singleNumber(int[] nums) {
        for (int i = 1; i < nums.length; i++) {
			nums[0] ^= nums[i];
		}
		return nums[0];
    }

III)

在看第三个,第三个跟第一个的关系比较密切:

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

For example:

Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].

Note:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
这个可以跟第一个联想,唯一不同的就是这个有两个数字出现一次。所以现在的目标就是转化到第一个,将数组分成两组,并且将两个数字分开,怎么分呢?

    public int[] singleNumber(int[] nums) {
        int n = 0;
        for (int i = 0; i < nums.length; i++) n ^= nums[i];
        n &= ~(n-1);
        int[] ret = new int[2];
        
        for (int i = 0; i < nums.length; i++) {
            if ((nums[i] & n) == 0) ret[0] ^= nums[i];
            else ret[1] ^= nums[i];
        }
           
        return ret;
    }
代码如上,先求整个数组的异或,则最终等价于两个出现一次的数字异或,然后我们根据异或的结果进行分组,很显然。如果异或的二进制表示中,有一位为1,则表示。这两个数字在此位置的二进制表示是不同的。所以就可以根据这个位来进行分组,然后就把问题转化为第一个问题了!

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 int singleNumber(int[] nums) {
    	int cntOne = 0;
    	int number = 0;
    	for (int i = 0; i < 32; i++) {
    		cntOne = 0;
    		for (int j = 0; j < nums.length; j++) {
    			if ((nums[j] & (1 << i)) != 0) cntOne++;
    		}
    		number |= (cntOne %= 3) << i;
    	}
    	return number;
    }

对int的每一位进行遍历判断。此时遍历数组统计每个位1,出现的次数,然后对3取余数就是出现一次的那个数在本位的次数,然后将这个32个0或1组合起来!





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值