美文网首页
[LeetCode]136. Single Number 异或用

[LeetCode]136. Single Number 异或用

作者: _AKI_Peroro_ | 来源:发表于2017-02-15 16:08 被阅读0次

题目:

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];
    }
};

相关文章

网友评论

      本文标题:[LeetCode]136. Single Number 异或用

      本文链接:https://www.haomeiwen.com/subject/kopqwttx.html