Skip to content

Commit bcdf4ad

Browse files
authored
Create 0219-contains-duplicate-ii.kt
1 parent e4c39e0 commit bcdf4ad

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

kotlin/0219-contains-duplicate-ii.kt

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
fun containsNearbyDuplicate(nums: IntArray, k: Int): Boolean {
3+
4+
val hs = HashSet<Int>()
5+
var left = 0
6+
var right = 0
7+
8+
while (right < nums.size) {
9+
10+
if (right - left > k) {
11+
hs.remove(nums[left])
12+
left++
13+
}
14+
15+
if (nums[right] in hs) return true
16+
17+
hs.add(nums[right])
18+
right++
19+
}
20+
21+
return false
22+
}
23+
}

0 commit comments

Comments
 (0)