题目
Given an array of integers, every element appears three times except for one. Find that single one.
代码
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
ones = [0]*32
negative = 0
for n in nums:
if bin(n)[0] == '-':
n = bin(n)[3:]
negative = (negative+1)%3
else:
n = bin(n)[2:]
for i in range(0,len(n)):
if n[i] == '1':
ones[32-(len(n)-i)] = (ones[32-(len(n)-i)] + 1)%3
res = int(''.join([str(one) for one in ones]),2)
if negative == 1:
res = -res
return res