Skip to content

Commit d962863

Browse files
authored
Create first-unique-number.py
1 parent e47866a commit d962863

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Python/first-unique-number.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Time: ctor: O(k)
2+
# add: O(1)
3+
# showFirstUnique: O(1)
4+
# Space: O(n)
5+
6+
import collections
7+
8+
9+
class FirstUnique(object):
10+
11+
def __init__(self, nums):
12+
"""
13+
:type nums: List[int]
14+
"""
15+
self.__q = collections.OrderedDict()
16+
self.__dup = set()
17+
for num in nums:
18+
self.add(num)
19+
20+
def showFirstUnique(self):
21+
"""
22+
:rtype: int
23+
"""
24+
if self.__q:
25+
return next(iter(self.__q))
26+
return -1
27+
28+
29+
def add(self, value):
30+
"""
31+
:type value: int
32+
:rtype: None
33+
"""
34+
if value not in self.__dup and value not in self.__q:
35+
self.__q[value] = None
36+
return
37+
if value in self.__q:
38+
self.__q.pop(value)
39+
self.__dup.add(value)
40+

0 commit comments

Comments
 (0)