-
Notifications
You must be signed in to change notification settings - Fork 62
/
ContainsDuplicateII.java
41 lines (37 loc) · 1012 Bytes
/
ContainsDuplicateII.java
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
38
39
40
41
package datastructure.array.leetcode;
import java.util.HashMap;
import java.util.Map;
/**
* @author roseduan
* @time 2020/10/30 9:43 上午
* @description 存在重复元素II
*/
public class ContainsDuplicateII {
/**
* 暴力法
*/
public boolean containsNearbyDuplicate1(int[] nums, int k) {
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] == nums[j] && j - i <= k) {
return true;
}
}
}
return false;
}
/**
* 使用一个哈希表
*/
public boolean containsNearbyDuplicate2(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i]) && i - map.get(nums[i]) <= k) {
return true;
} else {
map.put(nums[i], i);
}
}
return false;
}
}