class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
a = 0
for i in nums:
a ^= i
return a
- 用异或做
- 因为题目要求不能使用额外空间
- 另有用set做的,但是占用了额外空间
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return sum(set(nums)*2)-sum(nums)