Skip to content

Commit daade83

Browse files
committed
solution added
1 parent 7265996 commit daade83

File tree

7 files changed

+127
-0
lines changed

7 files changed

+127
-0
lines changed

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/Leetcode.iml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""
2+
You have a queue of integers, you need to retrieve the first unique integer in the queue.
3+
4+
Implement the FirstUnique class:
5+
6+
FirstUnique(int[] nums) Initializes the object with the numbers in the queue.
7+
int showFirstUnique() returns the value of the first unique integer of the queue, and returns -1 if there is no such integer.
8+
void add(int value) insert value to the queue.
9+
10+
11+
Example 1:
12+
13+
Input:
14+
["FirstUnique","showFirstUnique","add","showFirstUnique","add","showFirstUnique","add","showFirstUnique"]
15+
[[[2,3,5]],[],[5],[],[2],[],[3],[]]
16+
Output:
17+
[null,2,null,2,null,3,null,-1]
18+
19+
Explanation:
20+
FirstUnique firstUnique = new FirstUnique([2,3,5]);
21+
firstUnique.showFirstUnique(); // return 2
22+
firstUnique.add(5); // the queue is now [2,3,5,5]
23+
firstUnique.showFirstUnique(); // return 2
24+
firstUnique.add(2); // the queue is now [2,3,5,5,2]
25+
firstUnique.showFirstUnique(); // return 3
26+
firstUnique.add(3); // the queue is now [2,3,5,5,2,3]
27+
firstUnique.showFirstUnique(); // return -1
28+
29+
Example 2:
30+
31+
Input:
32+
["FirstUnique","showFirstUnique","add","add","add","add","add","showFirstUnique"]
33+
[[[7,7,7,7,7,7]],[],[7],[3],[3],[7],[17],[]]
34+
Output:
35+
[null,-1,null,null,null,null,null,17]
36+
37+
Explanation:
38+
FirstUnique firstUnique = new FirstUnique([7,7,7,7,7,7]);
39+
firstUnique.showFirstUnique(); // return -1
40+
firstUnique.add(7); // the queue is now [7,7,7,7,7,7,7]
41+
firstUnique.add(3); // the queue is now [7,7,7,7,7,7,7,3]
42+
firstUnique.add(3); // the queue is now [7,7,7,7,7,7,7,3,3]
43+
firstUnique.add(7); // the queue is now [7,7,7,7,7,7,7,3,3,7]
44+
firstUnique.add(17); // the queue is now [7,7,7,7,7,7,7,3,3,7,17]
45+
firstUnique.showFirstUnique(); // return 17
46+
47+
Example 3:
48+
49+
Input:
50+
["FirstUnique","showFirstUnique","add","showFirstUnique"]
51+
[[[809]],[],[809],[]]
52+
Output:
53+
[null,809,null,-1]
54+
55+
Explanation:
56+
FirstUnique firstUnique = new FirstUnique([809]);
57+
firstUnique.showFirstUnique(); // return 809
58+
firstUnique.add(809); // the queue is now [809,809]
59+
firstUnique.showFirstUnique(); // return -1
60+
61+
62+
63+
Constraints:
64+
65+
1 <= nums.length <= 10^5
66+
1 <= nums[i] <= 10^8
67+
1 <= value <= 10^8
68+
At most 50000 calls will be made to showFirstUnique and add.
69+
"""
70+
71+
72+
class FirstUnique:
73+
74+
def __init__(self, nums: List[int]):
75+
self.hmap = collections.Counter(nums)
76+
77+
def showFirstUnique(self) -> int:
78+
for k, v in self.hmap.items():
79+
if v == 1:
80+
return k
81+
return -1
82+
83+
def add(self, value: int) -> None:
84+
if not value in self.hmap.keys():
85+
self.hmap[value] = 1
86+
else:
87+
self.hmap[value] += 1
88+
89+
# Your FirstUnique object will be instantiated and called as such:
90+
# obj = FirstUnique(nums)
91+
# param_1 = obj.showFirstUnique()
92+
# obj.add(value)

0 commit comments

Comments
 (0)