首页 > 其他 > 详细

[LeetCode] Single Number III

时间:2015-08-17 17:06:20      阅读:141      评论:0      收藏:0      [点我收藏+]

Try to split all the numbers into two groups with each of the target one in different groups. Refer to this link for a nice explanation.

The code is written as follows.

 1 class Solution {
 2 public:
 3     vector<int> singleNumber(vector<int>& nums) {
 4         int t = 0, p, first = 0, second = 0;
 5         for (int num : nums) t ^= num;
 6         for (p = 0; p < 32; p++)
 7             if ((t >> p) & 1) break;
 8         for (int num : nums) {
 9             if ((num >> p) & 1) first ^= num;
10             else second ^= num;
11         }
12         return {first, second};
13     }
14 };

 

[LeetCode] Single Number III

原文:http://www.cnblogs.com/jcliBlogger/p/4736904.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!