-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstUniqueNumber.cpp
More file actions
46 lines (44 loc) · 1.09 KB
/
Copy pathFirstUniqueNumber.cpp
File metadata and controls
46 lines (44 loc) · 1.09 KB
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
class FirstUnique {
public:
//queue<int> q;
queue<int> uq;
unordered_map<int,int> m;
FirstUnique(vector<int>& nums) {
for(auto i:nums){
//q.push(i);
if(m[i]==0){
uq.push(i);
m[i]++;
}
else{
m[i]++;
if(i==uq.front()){
while(!uq.empty() and m[uq.front()]>1) uq.pop();
}
}
}
}
int showFirstUnique() {
if(!uq.empty()) return uq.front();
return -1;
}
void add(int value) {
//q.push(value);
if(m.find(value)==m.end()){
uq.push(value);
m[value]++;
}
else{
m[value]++;
if(value==uq.front()){
while(!uq.empty() and m[uq.front()]>1) uq.pop();
}
}
}
};
/**
* Your FirstUnique object will be instantiated and called as such:
* FirstUnique* obj = new FirstUnique(nums);
* int param_1 = obj->showFirstUnique();
* obj->add(value);
*/