[LintCode] Single Number 单独的数字

 

Given 2*n + 1 numbers, every numbers occurs twice except one, find it.

Example

Given [1,2,2,1,3,4,3], return 4

Challenge 

One-pass, constant extra space.

 

LeetCode上的原题,请参见我之前的博客Single Number

 

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int res = 0;
        for (auto num : nums) res ^= num;
        return res;
    }
};

 

posted @ 2016-11-06 13:55  Grandyang  阅读(556)  评论(0编辑  收藏  举报
Fork me on GitHub