[LeetCode]136. Single Number 异或用

2017-02-15  本文已影响0人  _AKI_Peroro_

题目:

Given an array of integers, every element appears twice except for one. Find that single one.

Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

解决:

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        for(int i = 1; i<nums.size(); ++i)
        {
            nums[0] ^= nums[i];
        }
        return nums[0];
    }
};
上一篇 下一篇

猜你喜欢

热点阅读