题目:
Given an array of integers, every element appearstwiceexcept for one. Find that single one.
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
解答:
int singleNumber(int A[], int n)
{
int ans = 0;
for (int i = 0; i < n; i++)
{
ans ^= A[i];//相同的数字经过异或运算结果为0
}
return ans;
}
于是,我深深领悟到了位运算符的魅力。