题目描述:

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;
}
}

  

  

Java [Leetcode 260]Single Number III的更多相关文章

  1. LeetCode 260. Single Number III(只出现一次的数字 III)

    LeetCode 260. Single Number III(只出现一次的数字 III)

  2. [LeetCode] 260. Single Number III 单独数 III

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

  3. [LeetCode#260]Single Number III

    Problem: Given an array of numbers nums, in which exactly two elements appear only once and all the ...

  4. LeetCode 260 Single Number III 数组中除了两个数外,其他的数都出现了两次,找出这两个只出现一次的数

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

  5. Leetcode 260 Single Number III 亦或

    在一个数组中找出两个不同的仅出现一次的数(其他数字出现两次) 同样用亦或来解决(参考编程之美的1.5) 先去取出总亦或值 然后分类,在最后一位出现1的数位上分类成 ans[0]和ans[1] a&am ...

  6. [LeetCode] 260. Single Number III(位操作)

    传送门 Description Given an array of numbers nums, in which exactly two elements appear only once and a ...

  7. leetcode 136 Single Number, 260 Single Number III

    leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...

  8. leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)

    136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...

  9. 【刷题-LeeetCode】260. Single Number III

    Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...

随机推荐

  1. SQL SERVER其它函数

    本篇文章还是学习<程序员的SQL金典>内容的记录,此次将讲解的是SQL SERVER常用的其它函数.(其它数据库这里就不罗列了,想看更多的可以关注<程序员的SQL金典>). 具 ...

  2. 2012 Asia JinHua Regional Contest

    Draw Something http://acm.hdu.edu.cn/showproblem.php?pid=4450 o(n)统计输入每个数的平方和. #include<cstdio> ...

  3. Codeforces Round #363 (Div. 2)->C. Vacations

    C. Vacations time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  4. uva 10651

    DP  主要是位运算的运用  因为只有12位 用一个数字代表一种装态 记忆化搜索 节约时间 /***************************************************** ...

  5. 微软发布WP SDK8.0 新增语音、应用内支付等原生API

    http://www.csdn.net/article/2012-10-31/2811338-windows-phone-8-sdk 京时间10月30日,微软在旧金山举行新一代手机操作系统Window ...

  6. Unity3D角色攻击范围判定和攻击判定

    原地址:http://www.unity蛮牛.com/blog-1801-479.html 第一种方法:运用点乘 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...

  7. jstl表达式使用方法

    1.jstl.jar  servlet.jar支持 2.jsp引入标签库 <%@ taglib prefix="c" uri="http://java.sun.co ...

  8. NSString+URLEncoding.h --使用Obj-C对数据等进行URLEncoding编码

    在Objective-c进行网络编程时,经常需要把数据转换成URLEncoding编码,如对+号编码后,变成%2b.这里我们给出一种实现. //NSString+URLEncoding.h #impo ...

  9. 安装xampp后,出现“Apache 2 Test Page powered by CentOS“

    因为是在本地测试,所以没有去考虑为什么会这样,考虑太多的原因.只要能运行就行. 所以网络搜索了一番. 最后,解决办法是: 1, 找到apachectl. 那么就在命令行敲find / -name ap ...

  10. spring aop通过joinpoint传递参数

    三.总结. 我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动传入,从JoinPoint中可以取得. 三.总结. 我们可以通过Advice中添加一个JoinPoint ...