-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSolution.cpp
50 lines (44 loc) · 1.01 KB
/
Solution.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
/*
* Created on Thu Apr 30 2020
*
* Title: Leetcode - First Unique Number
*
* Author: Vatsal Mistry
* Web: mistryvatsal.github.io
*/
#include <iostream>
#include <unordered_map>
#include <list>
#include <vector>
using namespace std;
class FirstUnique {
public:
unordered_map<int, list<int>::iterator> map;
list<int> nums_list;
FirstUnique(vector<int>& nums) {
for(auto n : nums) {
add(n);
}
}
int showFirstUnique() {
if(!nums_list.empty()) {
return nums_list.front();
}
return -1;
}
void add(int value) {
if(map.find(value) != map.end()) {
list<int>::iterator it = map[value];
if(it != nums_list.end()) {
nums_list.erase(it);
map[value] = nums_list.end();
}
}
else {
nums_list.push_back(value);
list<int>::iterator it = nums_list.end();
it--;
map[value] = it;
}
}
};