题目描述:
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?
思路:
首先想到用一个一维数组来记录,但是题目要求尽量不开新的空间。后来看了别人的代码。。。。原来全部亦或就可以了!!!!!!!
代码(python):
class Solution:
# @param A, a list of integer
# @return an integer
def singleNumber(self, A):
ans = 0
for i in A:
ans = ans ^ i
return ans