原题链接 https://leetcode.com/problems/single-number/
Given an array of integers, every element appears twice except for one. Find that single one.
找出唯一一个只出现一次的数。
因为其他数肯定出现两次 又因为A^A = 0, 0^B = B;
所以将数组中所有的数全部异或起来就找到了唯一出现的数
class Solution {
public:
int singleNumber(vector<int>& nums) {
int res = 0;
for (auto& it : nums)res ^= it;
return res;
}
};