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

}

};


要求O(n)并且不用额外的变量。非常巧妙的技巧。相同数取异或为0,所0和所有数的异或为本身,所以最后剩下的就是SingleNumber。