Fork me on GitHub

Single Number II

Description

https://leetcode.com/problems/single-number-ii/description/

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
ret = 0
count = 0
for item in nums:
count += ( (item >> 31) & 1 )
neg = count % 3

for position in range(32):
count = 0
for item in nums:
count += ( (item >> position) & 1 )
ret += (2 ** position) * ( (count % 3 + neg) % 2 )

if neg:
return -( ret + 1 )

return ret