leetoce 136
Question: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?
解题思路:因为a^a=0,所以在数组中,只要出现两次的数字异或的结果都为0。
class Solution {
public:
int singleNumber(vector<int>& nums) {
int len=nums.size();
for(int i=1;i<len;i++)
nums[0]=nums[0]^nums[i];//异或
return nums[0];
}
};