地址:http://oj.leetcode.com/problems/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?
代码:
class Solution {
public:
int singleNumber(int A[], int n) {
int ret = A[0];
for(int i = 1; i < n; ++i)
{
ret ^= A[i];
}
return ret;
}
};
python 源码:
class Solution:
# @param A, a list of integer
# @return an integer
def singleNumber(self, A):
ans = 0;
for i in A:
ans ^= i
return ans