lintcode 中等题:Single number III 落单的数III
题目
给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字。
给出 [1,2,2,3,4,4,5,3],返回 1和5
O(n)时间复杂度,O(1)的额外空间复杂度
解题
根据落单的数I,可以想到,所有的数进行异或运行的结果就是所求两个数的异或结果。
这个异或的结果,二进制数是1的位置说明这两个数对应的二进制位不相同。然后再怎么还原???
参考,理解的不是很透,找到第k位后,再判断数组中所以数的第k位是0 还是1,,出现两次的数对求解无影响,通过这个第k为把数组分成两类,也就把两个数分开了,这里的第k位在a、b中一定不相同的,一定是一个0一个1。
public class Solution {
/**
* @param A : An integer array
* @return : Two integers
*/
public List<Integer> singleNumberIII(int[] A) {
// write your code here
int axorb = 0;
LinkedList<Integer> res = new LinkedList<Integer>();
for( int i = 0; i <A.length;i++){
axorb ^= A[i];
}
int a = 0;
int b = 0;
int k = 0;
while( axorb % 2==0){
axorb >>= 1;
k++;
}
for(int i=0;i< A.length;i++){
int tmp =( A[i]>>k)%2;
if(tmp==0)
a ^= A[i];
else
b ^= A[i];
}
res.add(a);
res.add(b);
return res;
}
}
Java Code
总耗时: 3520 ms
class Solution:
"""
@param A : An integer array
@return : Two integer
"""
def singleNumberIII(self, A):
# write your code here
x = 0
for num in A:
x ^= num
a = 0
b = 0
k = 0
while x%2==0:
x = x>>1
k +=1
for num in A:
tmp = (num>>k)%2
if tmp==0:
a ^=num
else:
b ^=num
return [a,b]
Python Code
总耗时: 514 ms
当然对于这样的题目,利用HashMap是最简单不过的了。
public class Solution {
/**
* @param A : An integer array
* @return : Two integers
*/
public List<Integer> singleNumberIII(int[] A) {
// write your code here
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
LinkedList<Integer> res = new LinkedList<Integer>();
for(int i=0;i<A.length;i++){
if(map.containsKey(A[i])){
map.put(A[i],map.get(A[i]) + 1);
}else{
map.put(A[i],1);
}
if(map.get(A[i]) ==2)
map.remove(A[i]);
}
for(Integer k:map.keySet()){
res.add(k);
}
return res;
}
}
Java Code
总耗时: 4318 ms
优化一下
public class Solution {
/**
* @param A : An integer array
* @return : Two integers
*/
public List<Integer> singleNumberIII(int[] A) {
// write your code here
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
LinkedList<Integer> res = new LinkedList<Integer>();
for(int i=0;i<A.length;i++){
if(map.containsKey(A[i])){
map.remove(A[i]);
}else{
map.put(A[i],1);
}
}
for(Integer k:map.keySet()){
res.add(k);
}
return res;
}
}
Java Code
总耗时: 3995 ms
class Solution:
"""
@param A : An integer array
@return : Two integer
"""
def singleNumberIII(self, A):
# write your code here
d = {}
for num in A:
if num in d:
del d[num]
else:
d[num] = 1
return d.keys()
Python Code
总耗时: 586 ms
lintcode 中等题:Single number III 落单的数III的更多相关文章
- LeetCode 136. Single Number (落单的数)
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- lintcode 中等题:Singleton number II 落单的数 II
题目 落单的数 II 给出3*n + 1 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这个数字. 样例 给出 [1,1,2,3,3,3,2,2,4,1] ,返回 4 挑战 一次遍历,常数级 ...
- 84 落单的数 III
原题网址:http://www.lintcode.com/zh-cn/problem/single-number-iii/# 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到 ...
- lintcode 落单的数(位操作)
题目1 落单的数 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 链接:http://www.lintcode.com/zh-cn/problem/single ...
- LintCode笔记 - 82.落单的数
这一题相对简单,但是代码质量可能不是很好,我分享一下我的做题笔记以及做题过程给各位欣赏,有什么不足望各位大佬指出来 原题目,各位小伙伴也可以试着做一下 . 落单的数 中文English 给出 * n ...
- lintcode:落单的数
题目: 落单的数 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 样例 给出 [1,2,2,1,3,4,3],返回 4 挑战 一次遍历,常数级的额外空间复杂度 ...
- LinCode落单的数
easy 落单的数 查看执行结果 60% 通过 给出2*n + 1 个的数字,除当中一个数字之外其它每一个数字均出现两次.找到这个数字. 您在真实的面试中是否遇到过这个题? Yes 例子 给出 [1, ...
- lintcode-84-落单的数 III
84-落单的数 III 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字. 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度, ...
- 83 落单的数 II
原题网址:http://www.lintcode.com/zh-cn/problem/single-number-ii/ 给出3*n + 1 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这 ...
随机推荐
- sql中更新数据库用到declare @a in
declare @a in update TB_Class set @a=1,name='李小龙' where ID=1 这样就可以像更新哪个就更新哪个了 例如ibatisnet中需要更新的时候: & ...
- 例题6-3 Matrix Chain Multiplication ,Uva 442
这个题思路没有任何问题,但还是做了近三个小时,其中2个多小时调试 得到的经验有以下几点: 一定学会调试,掌握输出中间量的技巧,加强gdb调试的学习 有时候代码不对,得到的结果却是对的(之后总结以下常见 ...
- mongodb在ubuntu下的couldn‘t remove fs lock errno:9 Bad file descriptor的错误
按照官网上的安装方法: 在ubuntu系统下有可能出现如下错误: couldn't remove fs lock errno:9 Bad file descriptor 此时需要修改文件所有者 $ s ...
- 三种找回 linux root密码的方法
找回 linux root密码的三种方法 第1种方法: 1.在系统进入单用户状态,直接用passwd root去更改2.用安装光盘引导系统,进行linux rescue状态,将原来/分区挂接上来,作法 ...
- GridView中的荧光棒效果
使用 ASP.NET中的GridView控件的时候会遇到这个效果,当时觉得很神奇,其实就是两句代码的事儿,可是时间长了,有点儿忘了,今天练习一下, 顺便把删除的时候弹出js中的confirm对话框也写 ...
- 关于C语言中的typedef
在C语言中定义一个结构体,要最好使用typedef,使用typedef,实际上就是为我们的结构体起了一个新的名字,即定义了一个新的类型,在后面书写自己代码的时候,就可以直接使用自己定义的新的类型第一变 ...
- Discuz X1.5 利用添加好友处存储xss进行蠕虫worm扩散
Discuz X1.5 在添加好友的地方有处存储xss,借助此处xss跟用户交互可以进行蠕虫指数扩散. 位置在添加好友处 x完之后的效果 点击后触发 ok 借助此存储xss,我们进行worm传播,dz ...
- 用Python作GIS之四:Tkinter基本界面的搭建
Python下的主窗口可以定义如下:def start(self): #self.project = Project("temp") #self.pro ...
- .NET中class和struct的区别
1.引言 提起class和struct,我们首先的感觉是语法几乎相同,待遇却天壤之别.历史将接力棒由面向过程编程传到面向对象编程,class和struct也背负着各自的命运前行.在我认为,struct ...
- 初解DLL基本知识
1.DLL基本理论 在Windows操作系统中,几乎所有的内容都是以DLL的形式存在的. 1.DLL基本概念 语言程序要从目标代码(.obj)外部引用函数,可以通过俩种途径实现——静态链接和动态链接. ...