题目

给出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的更多相关文章

  1. LeetCode 136. Single Number (落单的数)

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

  2. lintcode 中等题:Singleton number II 落单的数 II

    题目 落单的数 II 给出3*n + 1 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这个数字. 样例 给出 [1,1,2,3,3,3,2,2,4,1] ,返回 4 挑战 一次遍历,常数级 ...

  3. 84 落单的数 III

    原题网址:http://www.lintcode.com/zh-cn/problem/single-number-iii/# 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到 ...

  4. lintcode 落单的数(位操作)

    题目1 落单的数 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 链接:http://www.lintcode.com/zh-cn/problem/single ...

  5. LintCode笔记 - 82.落单的数

    这一题相对简单,但是代码质量可能不是很好,我分享一下我的做题笔记以及做题过程给各位欣赏,有什么不足望各位大佬指出来 原题目,各位小伙伴也可以试着做一下 . 落单的数 中文English 给出 * n ...

  6. lintcode:落单的数

    题目: 落单的数 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 样例 给出 [1,2,2,1,3,4,3],返回 4 挑战 一次遍历,常数级的额外空间复杂度 ...

  7. LinCode落单的数

    easy 落单的数 查看执行结果 60% 通过 给出2*n + 1 个的数字,除当中一个数字之外其它每一个数字均出现两次.找到这个数字. 您在真实的面试中是否遇到过这个题? Yes 例子 给出 [1, ...

  8. lintcode-84-落单的数 III

    84-落单的数 III 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字. 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度, ...

  9. 83 落单的数 II

    原题网址:http://www.lintcode.com/zh-cn/problem/single-number-ii/ 给出3*n + 1 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这 ...

随机推荐

  1. ArcGIS API for JavaScript介绍

    ArcGIS API for JavaScript中的类是按照模块组织的,主要包含esri.esri/geometry.esri/renderers.esri/symbols.esri/symbols ...

  2. WCF 服务的ABC之绑定(六)

    绑定 Binding 绑定是开发人员控制WCF程序与其他消息交互的主要手段.从功能上看,绑定创建了通道工厂惑通道侦听器的堆栈对象.绑定直接惑间接创建的对象是WCF实现各种消息功能(例如,传输.安全性. ...

  3. DataGridView导入导出excel

    DataGridView导出到Excel #region 方法一 DateGridView导出到csv格式的Excel /// <summary> /// 导出数据到Excel.常用方法, ...

  4. 初测WIN10

    WIN10已经发布,通过百度直通车把WIN7升级成了WIN10,改变较大,不太习惯,用着不是很顺手. 吐槽几个问题 1.微软的Visual Studio 2015 Community版本,宣布是免费的 ...

  5. 反编译APK终结教程

    现在来教大家如何由网上下载的Android应用反编译为源码.如果你感兴趣,就来看一看吧.前提是你的电脑得已经配置好了java环境,如果没有配置好的话,下面我会附带一提,如果你还是不懂的话,那就上网搜一 ...

  6. ASP.NET中的TextBox下划线

    看到园子里一位同行写的 http://www.cnblogs.com/xiaopeng84/archive/2007/04/10/707093.html 但是没有贴出效果图,自己练了一下,贴出代码和效 ...

  7. c#中的结构与枚举

    结构 与c++不同的是,结构应该定义在命名空间或者类里面,成员变量叫字段,字段并且有访问控制符,每个字段前要加一个下划线 例子 using System; using System.Collectio ...

  8. C# 获得手机归属地功能

    今天通过查资料了解到web的页面抓取功能,应用HttpWebRequest和HttpWebResponse功能,从http://www.showji.com网站中抓取归属地信息 应该说这个方法是从别的 ...

  9. SSH时不需输入密码

      我这里有2台机器,一台装了Teradata数据库,ip是192.168.184.128,称它为teradata-pc:另一台装了Oracle数据库,ip地址是192.168.184.129,称它为 ...

  10. Sql Server 常用自定义函数

    -- select * from [dbo].[SplitToTable]('ADSF','|') -- 分解字符串 ALTER FUNCTION [dbo].[SplitToTable] ( @Sp ...