-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode219.java
More file actions
34 lines (29 loc) · 1.03 KB
/
LeetCode219.java
File metadata and controls
34 lines (29 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.HashSet;
public class LeetCode219 {
public static void main(String[] args) {
// 输入:nums = [1,2,3,1], k = 3
// 输出:true
System.out.println(new Solution219().containsNearbyDuplicate(new int[] { 1, 2, 3, 1 }, 3));
// 输入:nums = [1,0,1,1], k = 1
// 输出:true
System.out.println(new Solution219().containsNearbyDuplicate(new int[] { 1, 0, 1, 1 }, 1));
// 输入:nums = [1,2,3,1,2,3], k = 2
// 输出:false
System.out.println(new Solution219().containsNearbyDuplicate(new int[] { 1, 2, 3, 1, 2, 3 }, 2));
}
}
class Solution219 {
public boolean containsNearbyDuplicate(int[] nums, int k) {
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
if (set.contains(nums[i])) {
return true;
}
set.add(nums[i]);
if (set.size() > k) {
set.remove(nums[i - k]);
}
}
return false;
}
}