Java [Leetcode 260]Single Number 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?

解题思路:

这道题可以用HashMap来做,但是对于空间复杂度要求很高。题目要求的是常量的空间复杂度;

参考网上别人的思路,发现了一种非常巧妙的方法;

首先遍历整个数组,讲所有的值相异或,那么最后的值肯定是两个相异的值异或的结果。

这两个值不同,那么肯定有一位的二进制值不同,那么这个位相异或的结果肯定是这位的数值为1;

那么我们寻找这个数值位为1的位,

这里采用非常巧妙的方法:resTwo &= -resTwo; 因为int整数在java中是按照补码的方式来的,那么正数和它负值按位与的结果是原始最右边非0位的数字为1,其余位都为0;

这样我们把原来的数组分为两个部分,一部分是和resTwo按位与的结果为0的,另一部分的结果和resTwo按位与的结果为1的,并且那两个不相等的数分别落在这两个组中;

这样分别对两组的数异或,即可得到这两个数。

代码如下:

public class Solution {
    public int[] singleNumber(int[] nums) {
    	int resTwo = 0;
    	int[] res = new int[2];;
    	for(int i = 0; i < nums.length; i++){
    		resTwo ^= nums[i];
    	}
    	// find the rigthest bit which is not 0
    	resTwo &= -resTwo;
    	for(int i = 0; i < nums.length; i++){
    		if((nums[i] & resTwo) == 0){
    			res[0] ^= nums[i];
    		} else{
    			res[1] ^= nums[i];
    		}
    	}
    	return res;
    }
}

  

  

 

转载于:https://www.cnblogs.com/zihaowang/p/5241825.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值