136. Single Number
题目描述:
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?
思路
利用了异或操作的性质:
1,两个相同的数异或的结果为0
2,一个数和0异或的结果为这个数
代码实现
class Solution {
public:
int singleNumber(int A[], int n) {
int result = 0;
for(int i = 0;i < n;i++)
{
result ^= A[i];
}
return result;
}
};