forked from partho-maple/coding-interview-gym
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy path137_Single_Number_II.py
More file actions
36 lines (24 loc) · 817 Bytes
/
137_Single_Number_II.py
File metadata and controls
36 lines (24 loc) · 817 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
seenOnce, seenTwice = 0, 0
for num in nums:
# first appearance:
# add num to seen_once
# don't add to seen_twice because of presence in seen_once
# second appearance:
# remove num from seen_once
# add num to seen_twice
# third appearance:
# don't add to seen_once because of presence in seen_twice
# remove num from seen_twice
seenOnce = ~seenTwice & (seenOnce ^ num)
seenTwice = ~seenOnce & (seenTwice ^ num)
return seenOnce
sol = Solution()
input = [2,2,3,2]
output = sol.singleNumber(input)
print('Res: ', output)