-
Notifications
You must be signed in to change notification settings - Fork 0
/
FirstUnique.cpp
54 lines (51 loc) · 1.31 KB
/
FirstUnique.cpp
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
46
47
48
49
50
51
52
53
54
class FirstUnique {
public:
list<int> elements;
unordered_map<int , list<int>::iterator> mp;
unordered_map<int , int > banned ;
FirstUnique(vector<int>& nums) {
for(auto i : nums){
if(!mp.count(i)){
if(!banned[i]){
elements.push_front(i);
mp[i] = elements.begin();
}
}
else{
if(!banned[i]){
elements.erase(mp[i]);
mp.erase(i);
banned[i] = 1;
}
}
}
}
int showFirstUnique() {
if(!elements.empty()){
return elements.back();
}
else
return -1;
}
void add(int value) {
if(!mp.count(value)){
if(!banned[value]){
elements.push_front(value);
mp[value] = elements.begin();
}
}
else{ //6 //6 //6
if(!banned[value]){
elements.erase(mp[value]);
mp.erase(value);
banned[value] =1;
}
}
}
};
/**
* Your FirstUnique object will be instantiated and called as such:
* FirstUnique* obj = new FirstUnique(nums);
* int param_1 = obj->showFirstUnique();
* obj->add(value);
*/