Description
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?
Explain
只有一个数出现一次,其他的数出现两次,一个数异或本身为0,则全部数异或一遍即可找到
Code
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
res = 0
for i in nums:
res ^= i
return res