forked from forging2012/JavaArithmetic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode219.java
More file actions
37 lines (25 loc) · 870 Bytes
/
LeetCode219.java
File metadata and controls
37 lines (25 loc) · 870 Bytes
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
35
36
37
package LeetCode;
import java.util.HashSet;
public class LeetCode219 {
// https://leetcode.com/problems/contains-duplicate-ii/description/
// 时间复杂度: O(n)
// 空间复杂度: O(k)
public boolean containsNearbyDuplicate(int[] nums, int k) {
if(nums == null || nums.length <= 1)
return false;
if(k <= 0)
return false;
// nums = [1,2,3,4,1], k = 3
// nums = [1,2,3,1,2,3], k = 2
HashSet<Integer> record = new HashSet<Integer>();
for(int i = 0 ; i < nums.length; i ++){
if(record.contains(nums[i]))
return true;
record.add(nums[i]);
// 当map的大小大于k+1,那就减去最左边的元素
if(record.size() == k + 1)
record.remove(nums[i-k]);
}
return false;
}
}