题目描述:

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. C# memcache

    概述 memcache是一套开放源的分布式高速缓存系统.由服务端和客户端组成,以守护程序(监听)方式运行于一个或多个服务器中,随时会接收客户端的连接和操作.memcache主要把数据对象缓存到内存中, ...

  2. ELF

    http://www.360doc.com/content/11/0826/13/7588214_143424472.shtml 链接,装载都是基于数据结构ELF.

  3. WSDL 文档解析

    学习webservice,就离不了WSDL文档,他是我们开发WebService的基础,虽说,现在现在有许多WebService的开源框架使得我们可以根据WSDL生成客户端代码,但是,了解WSDL文档 ...

  4. 剑指offer--面试题7

    //两个栈实现一个队列 #include<stack> //STL #include<iostream> using namespace std; template<cl ...

  5. POJ2251Dungeon Master

    http://poj.org/problem?id=2251 题意 : 就是迷宫升级版,从以前的一个矩阵也就是一层,变为现在的L层," . "是可以走,但是“#”不可以走,从S走到 ...

  6. mapreduce程序编写(WordCount)

    折腾了半天.终于编写成功了第一个自己的mapreduce程序,并通过打jar包的方式运行起来了. 运行环境: windows 64bit eclipse 64bit jdk6.0 64bit 一.工程 ...

  7. 220 DIV2 A. Inna and Pink Pony

    Inna and Pink Pony 输入n,m,i,j,a,b 可以看成n行m列的矩阵,起点(i,j),每次移动(a,b),(-a,-b),(-a,b),(a,-b) 可移动到(1,m),(n,1) ...

  8. Java快速排序 分别以数组0位作为基准 和最后一位作为基准的排序演示

    package util; public class Pub { public static void beforeSort(int[] arr){ System.out.println(" ...

  9. 合并傻子//区间dp

    P1062 合并傻子 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 从前有一堆傻子,钟某人要合并他们~但是,合并傻子是要掉RP的...... 描述 在一个园 ...

  10. 分布式内存对象缓存系统Memcached-概述

    全面掌握Memcached 1.       概述 Memcached是danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,是为了加快网站http://www. ...