只有一个数字出现一遍,其他出现两遍
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return reduce(operator.xor,nums)
只有一个数字出现一边,其他出现三遍
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
one = 0
two = 0
three = 0
for i in range(len(nums)):
two |= one&nums[i]
one ^= nums[i]
three = two&one
one &= ~three
two &= ~three
return one