-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 23
34 lines (27 loc) · 1.05 KB
/
Day 23
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
Que: https://leetcode.com/problems/contains-duplicate-ii
Case 1: Input: nums = [1,2,3,1], k = 3
Output: true
Case 2: Input: nums = [1,0,1,1], k = 1
Output: true
Case 3: Input: nums = [1,2,3,1,2,3], k = 2
Output: false
Constraints:
1 <= nums.length <= 105
-109 <= nums[i] <= 109
0 <= k <= 105
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
set<int> myset;
for(int i = 0; i < nums.size(); i++){
//window : [i-k, i]
//so (i-k-1)th goes out of the window, need to remove it
if(i-k-1 >= 0) myset.erase(nums[i-k-1]);
/*
The single element versions (1) return a pair, with its member pair::first set to an iterator pointing to either the newly inserted element or to the equivalent element already in the set. The pair::second element in the pair is set to true if a new element was inserted or false if an equivalent element already existed.
*/
if(!myset.insert(nums[i]).second) return true;
}
return false;
}
};