-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSolution.cpp
46 lines (42 loc) · 1.08 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
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <tuple>
#include <deque>
#include <unordered_map>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map<int, vector<int>> map;
for (int j = 0; j < nums.size(); j ++) {
if (map.find(nums[j]) != map.end()) {
int i = map[nums[j]].back();
// cout << "find i = " << i << endl;
// cout << "now j = " << j << endl;
if ( j - i <= k) {
return true;
} else {
map[nums[j]].push_back(j);
}
} else {
map[nums[j]] = {j};
// cout << "inserting " << j << endl;
}
}
return false;
}
};
int main() {
Solution a;
vector<int> v = {1,2,3,1,2,3};
int k = 2;
auto res = a.containsNearbyDuplicate(v, k);
cout << res;
return 0;
}