File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 3131151|[ Reverse Words in a String] ( ./0151-reverse-words-in-a-string.js ) |Medium|
3232152|[ Maximum Product Subarray] ( ./0152-maximum-product-subarray.js ) |Medium|
3333217|[ Contains Duplicate] ( ./0217-contains-duplicate.js ) |Easy|
34+ 219|[ Contains Duplicate II] ( ./0219-contains-duplicate-ii.js ) |Easy|
3435226|[ Invert Binary Tree] ( ./0226-invert-binary-tree.js ) |Easy|
3536263|[ Ugly Number] ( ./0263-ugly-number.js ) |Easy|
3637264|[ Ugly Number II] ( ./0264-ugly-number-ii.js ) |Medium|
Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments