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 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这 ...
随机推荐
- oracle中的存储过程例子
用了两年Oracle还没写过存储过程,真是十分惭愧,从今天开始学习Oracle存储过程,完全零起点,争取每日一篇学习笔记,可能开始认识的不全面甚至有错误,但坚持下来一定会有收获. . 建立一个存储过程 ...
- 关于Resources.LoadAssetAtPath
返回的是Object, 返回所在资源路径上的一个资源, 只能应用在unity的编辑器模式下,安卓等平台无效 static function LoadAssetAtPath (assetPath : s ...
- CHROME下去掉保存密码后输入框变成黄色背景样式
之前没遇到过这种情况,现在打开这个页面后,手机号和密码都已经输入了,而且还显示的是黄色背景,清了下cookie,没有解决问题.请教了下大神,先把方法整理到这儿. 用代码审查看了input样式有如下样式 ...
- asp.net 中给gridview添加自动序号
第一种方式,直接在Aspx页面GridView模板列中.这种的缺点是到第二页分页时又重新开始了. 代码如下: <asp:TemplateField HeaderText="序号&quo ...
- android camera2 Api(转载)
现在的手机一般都会提供相机功能,有些相机的镜头甚至支持1000万以上像素,有些甚至支持光学变焦,这些手机已经变成了专业数码相机.为了充分利用手机上的相机功能,Android应用可以控制拍照和录制视频. ...
- Jquery ajax请求导出Excel表格
直接贴代码吧 $("#btn-export").click(function(){ var exportExcel = "export_excel"; data ...
- 【Leetcode】 - Divide Two Integers 位运算实现整数除法
实现两个整数的除法,不许用乘法.除法和求模.题目被贴上了BinarySearch,但我没理解为什么会和BinarySearch有关系.我想的方法也和BS一点关系都没有. 很早以前我就猜想,整数的乘法是 ...
- 框架优化系列文档:SVN中非版本控制文件忽略上传的设置
对于SVN代码库,只应该上传源代码.资源文件等内容进行版本管理,通常编译后的二进制文件.程序包等生成产物是不应该放到SVN上做版本管理的.因此在svn的客户端工具中设置svn的属性:svn:ignor ...
- matlab fscanf用法
matlab fscanf用法 matlab中的fscanf的用法如下: A=fscanf(fid,format)[A, count]=fscanf(fid,format,size) [A, coun ...
- c++中new和delete的使用方法
c++中new和delete的使用方法 new和delete运算符用于动态分配和撤销内存的运算符 new用法: 1. 开辟单变量地址空间 1)new int; //开辟一个存放数组的存储空间 ...