Key:两个相同的整数异或为0,而0与非0的整数异或为该数,由于数组中只有一个数出现一次,因此将所有的证书异或即可得到答案,c++的异或标志为^
class Solution {
public:
int singleNumber(int A[], int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if (n<1){
return 0;
}
int key = A[0] ;
for (int i = 1; i < n ; i++){
key = key^A[i];
}
return key;
}
};