We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e47866a commit d962863Copy full SHA for d962863
Python/first-unique-number.py
@@ -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