Skip to content

Commit 22d07ed

Browse files
committed
Add solution #219
1 parent 66c3799 commit 22d07ed

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
151|[Reverse Words in a String](./0151-reverse-words-in-a-string.js)|Medium|
3232
152|[Maximum Product Subarray](./0152-maximum-product-subarray.js)|Medium|
3333
217|[Contains Duplicate](./0217-contains-duplicate.js)|Easy|
34+
219|[Contains Duplicate II](./0219-contains-duplicate-ii.js)|Easy|
3435
226|[Invert Binary Tree](./0226-invert-binary-tree.js)|Easy|
3536
263|[Ugly Number](./0263-ugly-number.js)|Easy|
3637
264|[Ugly Number II](./0264-ugly-number-ii.js)|Medium|

Diff for: solutions/0219-contains-duplicate-ii.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 219. Contains Duplicate II
3+
* https://leetcode.com/problems/contains-duplicate-ii/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer array `nums` and an integer `k`, return `true` if there are
7+
* two distinct indices `i` and `j` in the array such that `nums[i] == nums[j]`
8+
* and `abs(i - j) <= k`.
9+
*/
10+
11+
/**
12+
* @param {number[]} nums
13+
* @param {number} k
14+
* @return {boolean}
15+
*/
16+
var containsNearbyDuplicate = function(nums, k) {
17+
const map = new Map();
18+
19+
for (let i = 0; i < nums.length; i++) {
20+
if (Math.abs(i - map.get(nums[i])) <= k) {
21+
return true;
22+
}
23+
map.set(nums[i], i);
24+
}
25+
26+
return false;
27+
};

0 commit comments

Comments
 (0)