Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 32f3f53

Browse files
authored
Merge pull request neetcode-gh#1461 from Kongx231/create-211-duplicate-numbers-2
Create 219-Contains-Duplicate-II.cpp
2 parents 10d2fbf + 8bd1844 commit 32f3f53

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

cpp/219-Contains-Duplicate-II.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
bool containsNearbyDuplicate(vector<int>& nums, int k) {
4+
unordered_map<int,int> number_map;
5+
for (int i = 0; i < nums.size(); ++i) {
6+
int num = nums[i];
7+
if (number_map.find(num) != number_map.end() && i - number_map[num] <= k) {
8+
return true;
9+
}else {
10+
number_map[num] = i;
11+
}
12+
}
13+
return false;
14+
}
15+
};

0 commit comments

Comments
 (0)