Skip to content

Commit c4d6a5c

Browse files
committed
Adding 0219-contains-duplicate-ii.java
1 parent 92ec5e7 commit c4d6a5c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

java/0219-contains-duplicate-ii.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public boolean containsNearbyDuplicate(int[] nums, int k) {
3+
4+
Set<Integer> window = new HashSet<>();
5+
int left = 0;
6+
for (int right = 0; right < nums.length; right++) {
7+
8+
if (window.size() > k) {
9+
window.remove(nums[left]);
10+
left++;
11+
}
12+
13+
if (window.contains(nums[right])) {
14+
return true;
15+
}
16+
17+
window.add(nums[right]);
18+
}
19+
20+
return false;
21+
}
22+
}

0 commit comments

Comments
 (0)