[Leetcode] Single Number III, Solution
2015-10-17 13:59
323 查看
Given an array of numbers
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
[Thought]关于single number的解法,因为只有一个未知数,比较直观:http://fisherlei.blogspot.com/2013/11/leetcode-single-number-solution.html。
这题有两个未知数,直接做异或肯定是不行的,那么如何通过一些变换把这道题分解开,使得可以应用Single Number的解法来做,才是这个题目有意思的地方。首先,对于数组A, 假设存在b,c两个数字,在数组中只出现了一次,那么对于整个数组进行异或操作的话,^[A] = b^c , 因为其他的数因为出现了两次,异或的过程中就被清零了。
但是仅仅通过最后异或出来的值,是没办法求出b和c的值的,但是足以帮我们把b和c划分到不同的子数组中去。
一个整数有32位bit,对于b和c,除非两者是相同的数,否则一定存在第K位bit,两者是不同的。看下面的例子,
当找到这个K以后,就可以按照第K位bit是否等于1,将A数组划分成两个子数组,而这两个子数组分别包含了b和c,那么剩下的就只需要把single number的算法直接应用到这两个子数组上,就可以得到b和c了。
[code]1: class Solution { 2: public: 3: vector<int> singleNumber(vector<int>& nums) { 4: int length = nums.size(); 5: // get the xor result of the array, b ^ c 6: int xor_result = 0; 7: for(int i =0; i< length; i++) { 8: xor_result ^= nums[i]; 9: } 10: // get the K of first bit, which equals 1 11: int first_one_index = 0; 12: for(first_one_index =0; first_one_index< 32; first_one_index++) { 13: if((xor_result>>first_one_index) & 1 == 1) { 14: break; 15: } 16: } 17: // use k to split the array into two part 18: // xor the sub array, if the element's Kth bit also equals 1, b 19: int xor_twice = 0; 20: for(int i =0; i< length; i++) { 21: if((nums[i]>>first_one_index) & 1 == 1) { 22: xor_twice ^= nums[i]; 23: } 24: } 25: // with b, easy to get c by math 26: vector<int> result = {xor_twice, xor_result ^ xor_twice }; 27: return result; 28: } 29: };Git hub: https://github.com/codingtmd/leetcode/blob/master/src/Single_Number_III.cpp
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?
[Thought]关于single number的解法,因为只有一个未知数,比较直观:http://fisherlei.blogspot.com/2013/11/leetcode-single-number-solution.html。
这题有两个未知数,直接做异或肯定是不行的,那么如何通过一些变换把这道题分解开,使得可以应用Single Number的解法来做,才是这个题目有意思的地方。首先,对于数组A, 假设存在b,c两个数字,在数组中只出现了一次,那么对于整个数组进行异或操作的话,^[A] = b^c , 因为其他的数因为出现了两次,异或的过程中就被清零了。
但是仅仅通过最后异或出来的值,是没办法求出b和c的值的,但是足以帮我们把b和c划分到不同的子数组中去。
一个整数有32位bit,对于b和c,除非两者是相同的数,否则一定存在第K位bit,两者是不同的。看下面的例子,
当找到这个K以后,就可以按照第K位bit是否等于1,将A数组划分成两个子数组,而这两个子数组分别包含了b和c,那么剩下的就只需要把single number的算法直接应用到这两个子数组上,就可以得到b和c了。
[code]1: class Solution { 2: public: 3: vector<int> singleNumber(vector<int>& nums) { 4: int length = nums.size(); 5: // get the xor result of the array, b ^ c 6: int xor_result = 0; 7: for(int i =0; i< length; i++) { 8: xor_result ^= nums[i]; 9: } 10: // get the K of first bit, which equals 1 11: int first_one_index = 0; 12: for(first_one_index =0; first_one_index< 32; first_one_index++) { 13: if((xor_result>>first_one_index) & 1 == 1) { 14: break; 15: } 16: } 17: // use k to split the array into two part 18: // xor the sub array, if the element's Kth bit also equals 1, b 19: int xor_twice = 0; 20: for(int i =0; i< length; i++) { 21: if((nums[i]>>first_one_index) & 1 == 1) { 22: xor_twice ^= nums[i]; 23: } 24: } 25: // with b, easy to get c by math 26: vector<int> result = {xor_twice, xor_result ^ xor_twice }; 27: return result; 28: } 29: };Git hub: https://github.com/codingtmd/leetcode/blob/master/src/Single_Number_III.cpp
相关文章推荐
- MERGE 用法
- QYQ在艾泽拉斯
- 2D Delaunay Refinement (2D Delaunay 细化)
- 2015-郭辉-项目质量管理+项目人力资源管理
- Unity3d特效DrawCall优化介绍
- could not disable core file generation for pid 18102: Operation not permitted
- Java 八大排序之——冒泡排序
- HTML与XHTML差额
- 监听textarea输入文本变化,让textarea高度自增长
- Unity3D研究院之游戏对象的访问绘制线与绘制面详解(十七)
- android全屏显示,去掉标题栏和信息栏
- eclipse项目更换数据库
- wireshark过滤规则
- 20135323符运锦-信息安全系统设计基础第六周学习总结
- Bootstrap 导航 标题栏
- hdoj Reorder the Books 5500 (技巧) 好题
- Amazon OA2准备——迷宫
- windows下安装beautifulsoup4
- jQuery源码分析之从off方法看unbind,undelegate方法
- 3子棋游戏