码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Single Number III

时间:2015-08-17 17:08:18      阅读:144      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!