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^b)^a=b。
这样,将所有元素依次与第一个值做异或操作,便能够得到唯一的那个值。
int singleNumber(vector & nums) { for (int i = 1; i < nums.size();i++) { nums[0] ^= nums[i]; } return nums[0]; }