描述
给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字。
您在真实的面试中是否遇到过这个题?
是
样例
给出 [1,2,2,1,3,4,3],返回 4
挑战
一次遍历,常数级的额外空间复杂度
一次遍历想得出最终结果。可以考虑如何抵消掉掉相同的数,位运算中的异或给了思路,相同为0,相异为1。
class Solution {
public:
/**
* @param A: An integer array
* @return: An integer
*/
int singleNumber(vector<int> &A) {
// write your code here
int a=0;
for(int i:A){
a^=i;
}
return a;
}
};