-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contains_Duplicate_II.java
44 lines (41 loc) · 1.24 KB
/
Contains_Duplicate_II.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
42
43
44
package com.leet_code;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Contains_Duplicate_II {
public static void main(String[] args) {
int[] arr = {99,99};
int k = 2;
System.out.println(containsNearbyDuplicate(arr,k));
}
public static boolean containsNearbyDuplicate(int[] nums, int k) {
Map<Integer,Integer> hm=new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (!hm.containsKey(nums[i])) {
hm.put(nums[i],i);
}else {
int p=hm.get(nums[i]);
if ((p -i)<=k) {
return true;
}else {
hm.put(nums[i],i);
}
}
}
return false;
}
public static boolean ontainsNearbyDuplicate(int[] nums, int k) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
int prevIndex = map.get(nums[i]);
if ((i - prevIndex) <= k) {
return true;
}
}
map.put(nums[i], i);
}
return false;
}
}