forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst-unique-number.py
More file actions
39 lines (32 loc) · 810 Bytes
/
first-unique-number.py
File metadata and controls
39 lines (32 loc) · 810 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
32
33
34
35
36
37
38
# Time: ctor: O(k)
# add: O(1)
# showFirstUnique: O(1)
# Space: O(n)
import collections
class FirstUnique(object):
def __init__(self, nums):
"""
:type nums: List[int]
"""
self.__q = collections.OrderedDict()
self.__dup = set()
for num in nums:
self.add(num)
def showFirstUnique(self):
"""
:rtype: int
"""
if self.__q:
return next(iter(self.__q))
return -1
def add(self, value):
"""
:type value: int
:rtype: None
"""
if value not in self.__dup and value not in self.__q:
self.__q[value] = None
return
if value in self.__q:
self.__q.pop(value)
self.__dup.add(value)