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)的时间复杂度,O(1)的空间复杂度。
异或:0与非0异或均为非0
代码如下:
class Solution:
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
res = 0
for i in nums:
res ^=i
return res