-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1429.first-unique-number.java
45 lines (38 loc) · 1.04 KB
/
1429.first-unique-number.java
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
39
40
41
42
43
44
45
/*
* @lc app=leetcode id=1429 lang=java
*
* [1429] First Unique Number
*/
// @lc code=start
class FirstUnique {
// LinkedHashSet support O(1) remove, even element is in the middle
Set<Integer> setQueue = new LinkedHashSet<>();
Map<Integer, Boolean> isUnique = new HashMap<>();
public FirstUnique(int[] nums) {
for (int num : nums) {
this.add(num);
}
}
public int showFirstUnique() {
if (!setQueue.isEmpty()) {
return setQueue.iterator().next();
} else {
return -1;
}
}
public void add(int value) {
if (!isUnique.containsKey(value)) {
isUnique.put(value, true);
setQueue.add(value);
} else if (isUnique.get(value)) {
isUnique.put(value, false);
setQueue.remove(value);
}
}
}
/**
* Your FirstUnique object will be instantiated and called as such: FirstUnique
* obj = new FirstUnique(nums); int param_1 = obj.showFirstUnique();
* obj.add(value);
*/
// @lc code=end