Leetcode Single Number III
2015-10-10 12:39
513 查看
Given an array of numbers
For example:
Given
Note:
The order of the result is not important. So in the above example,
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
2. 用Bit Manipulation。
首先计算nums数组中所有数字的异或,记为xor 令lowbit = xor & -xor,lowbit的含义为xor从低位向高位,第一个非0位所对应的数字 例如假设xor = 6(二进制:0110),则-xor为(二进制:1010,-6的补码,two's complement) 则lowbit = 2(二进制:0010)
根据异或运算的性质,“同0异1” 记只出现一次的两个数字分别为a与b,可知a & lowbit与b & lowbit的结果一定不同
通过这种方式,即可将a与b拆分开来。
Java code:
方法一:
方法二:
Reference:
1. https://leetcode.com/discuss/52351/c-o-n-time-o-1-space-9-line-solution-with-detail-explanation
2. https://leetcode.com/discuss/52521/share-two-java-solution-%EF%BC%9Ad
3. http://bookshadow.com/weblog/2015/08/17/leetcode-single-number-iii/
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?
解题思路:
1. 用HashSet ,速度慢。2. 用Bit Manipulation。
首先计算nums数组中所有数字的异或,记为xor 令lowbit = xor & -xor,lowbit的含义为xor从低位向高位,第一个非0位所对应的数字 例如假设xor = 6(二进制:0110),则-xor为(二进制:1010,-6的补码,two's complement) 则lowbit = 2(二进制:0010)
根据异或运算的性质,“同0异1” 记只出现一次的两个数字分别为a与b,可知a & lowbit与b & lowbit的结果一定不同
通过这种方式,即可将a与b拆分开来。
Java code:
方法一:
public int[] singleNumber(int[] nums) { Set<Integer> set = new HashSet<Integer>(); int[] result = new int[2]; for(int i = 0; i< nums.length; i++){ if(set.contains(nums[i])){ set.remove(nums[i]); }else{ set.add(nums[i]); } } Iterator it = set.iterator(); result[0] = (int)it.next(); result[1] = (int)it.next(); return result; }
方法二:
public int[] singleNumber(int[] nums) { int record = 0; for(int num: nums){ record ^= num; } record &= -record; int[] result = {0, 0}; for(int num: nums){ if((num & record) == 0){ result[0] ^= num; }else{ result[1] ^= num; } } return result; }
Reference:
1. https://leetcode.com/discuss/52351/c-o-n-time-o-1-space-9-line-solution-with-detail-explanation
2. https://leetcode.com/discuss/52521/share-two-java-solution-%EF%BC%9Ad
3. http://bookshadow.com/weblog/2015/08/17/leetcode-single-number-iii/
相关文章推荐
- Node.js 中的copy
- 贝叶斯定理
- 使用mp4v2将H264+AAC合成mp4文件
- 利用Aspose.Cells 组件导出数据到excel
- 数据适配器 BaseAdapter
- 贝叶斯定理
- Dell R720服务器安装操作系统
- 奖学金申请表--用html中的table实现
- jquery倒计时
- 集群服务器环境下安装Caffe深度学习库(GPU)
- Android studio 查找快捷键对应的英文名字
- 支付宝的性能测试
- sql 增加非空字段并设置初始值
- 完整详细的说明GCD列(一)dispatch_async;dispatch_sync;dispatch_async_f;dispatch_sync_f
- 强制Android应用使用某个Locale的方法
- printf的妙用--谈printf的各种格式
- MongoDB中关于64位整型存储解决方案
- C#监控-机器资源监控:CPU、内存、C盘、D盘资源监控及邮件预警
- C/C++中调用matlab引擎计算
- C/C++中调用matlab引擎计算