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:
- The order of the result is not important. So in the above example,
[5, 3]
is also correct. - 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的更多相关文章
- LeetCode 260. Single Number III(只出现一次的数字 III)
LeetCode 260. Single Number III(只出现一次的数字 III)
- [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 ...
- [LeetCode#260]Single Number III
Problem: Given an array of numbers nums, in which exactly two elements appear only once and all the ...
- LeetCode 260 Single Number III 数组中除了两个数外,其他的数都出现了两次,找出这两个只出现一次的数
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- Leetcode 260 Single Number III 亦或
在一个数组中找出两个不同的仅出现一次的数(其他数字出现两次) 同样用亦或来解决(参考编程之美的1.5) 先去取出总亦或值 然后分类,在最后一位出现1的数位上分类成 ans[0]和ans[1] a&am ...
- [LeetCode] 260. Single Number III(位操作)
传送门 Description Given an array of numbers nums, in which exactly two elements appear only once and a ...
- leetcode 136 Single Number, 260 Single Number III
leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- 【刷题-LeeetCode】260. Single Number III
Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...
随机推荐
- K短路
K短路 用dijsktra+A*启发式搜索当点v第K次出堆的时候,这时候求得的路径是k短路.A*算法有一个启发式函数f(p)=g(p)+h(p), 即评估函数=当前值+当前位置到终点的最短距离g(p) ...
- Matlab计算两集合间的海明距离
一.问题描述 B1[1 2 3 4 5 6 7 8 9] B2[12 13 14 21 31 41 51 1 1 81 1 1] 两个十进制矩阵,行数不一样,分别是n1和n2,列数必须一致,为nwo ...
- PE工具
PE编辑工具 Stud_PE v. 2.4.0.1 PE工具,用来学习PE格式十分方便. http://www.cgsoftlabs.ro/ 汉化版:http://bbs.pediy.com/show ...
- HTML5 动画引擎 小记
国内: Cocos2d-x js版本 layabox Egret Sirius2D lufylegend.js Fireball 国外: CreateJS(EaselJS.TweenJS)http ...
- Implicitly Typed Local Variables
Implicitly Typed Local Variables It happens time and time again: I’ll be at a game jam, mentoring st ...
- linux服务器重启服务命令说明文档
(前提是电脑上面已经安装好了ssh软件~!)输入ip,用户名,端口(默认22) 输入密码,登陆成功之后, 转入到/usr/local/tomcat/bin 目录,输入命令行: [root@yangch ...
- JavaScript Madness: Dynamic Script Loading
Introduction I've developed some pretty seriously Javascript intensive sites, where the sheer quanti ...
- java基础知识回顾之---java String final类普通方法的应用之“按照字节截取字符串”
/*需求:在java中,字符串“abcd”与字符串“ab你好”的长度是一样,都是四个字符.但对应的字节数不同,一个汉字占两个字节.定义一个方法,按照最大的字节数来取子串.如:对于“ab你好”,如果取三 ...
- java基础知识回顾之java Thread类学习(三)--java线程实现常见的两种方式实现好处:
总结:实现Runnable接口比继承Thread类更有优势: 1.因为java只能单继承,实现Runnable接口可以避免单继承的局限性 2.继承Thread类,多个线程不能处理或者共享同一个资源,但 ...
- java基础知识回顾之---java String final类普通方法的应用之“子串在整串中出现的次数”
/* * 2 一个子串在整串中出现的次数. * "loveerlovetyloveuiloveoplove" * 思路: * 1,要找的子串是否存在,如果存在获取其出现的位置.这个 ...