题目描述:

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. 牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?

    // test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  2. bzoj 3907: 网格 组合数学

    3907: 网格 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 13  Solved: 7[Submit][Status][Discuss] Descr ...

  3. 基于 Eclipse 平台的代码生成技术

    ------------------------------------------------------------------ 转自http://www.ibm.com/developerwor ...

  4. MyEclipse 简单快捷键

    1) Ctrl+/  注释当前行,再按则取消注释 2) Ctrl+M切换窗口的大小 3) Ctrl+Shift+O作用是缺少的Import语句被加入,多余的Import语句被删除. 4)Alt+/ 代 ...

  5. Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  6. Boost简介

    原文链接:  吴豆豆http://www.cnblogs.com/gdutbean/archive/2012/03/30/2425201.html Boost库 Boost库是为C++语言标准库提供扩 ...

  7. POJ 3461 Oulipo(字符串匹配,KMP算法)

    题意:给出几组数据,每组有字符串W和T,问你W在T中出现几次. 思路:字符串长度很大,用KMP算法. 一开始写的是:调用KMP算法查找W在T中是否匹配,若匹配,则个数+1.则接下来T的索引移动相应的距 ...

  8. 利用hadoop自带程序运行wordcount

    1.启动hadoop守护进程 bin/start-all.sh 2.在hadoop的bin目录下建立一个input文件夹 JIAS-MacBook-Pro:hadoop- jia$ mkdir inp ...

  9. java核心技术记录之java术语

    术语名 缩写 解释 Java Development Kit JDK 编写java程序的程序员使用的软件 Java Runtime Environment JRE 运行java程序的用户使用的软件 S ...

  10. linux入门教程(三) Linux操作系统的安装

    因为笔者一直都是使用CentOS,所以这次安装系统也是基于CentOS的安装.把光盘插入光驱,设置bios光驱启动.进入光盘的欢迎界面. 其中有两个选项,可以直接按回车,也可以在当前界面下输入 lin ...