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?
大意:
找出数组中只出现一次的元素,其余的都是出现两次
注意:
你的算法需要有一个线性的复杂度。即o(n)。最好不要使用额外的memory来实现
思路: 1 ^ 1 ^ 2 ^ 2 ^ 3 = 3
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
result = nums[0]
for i in xrange(1, len(nums)):
result = result ^ nums[i]
return result