-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
59 lines (51 loc) · 1.49 KB
/
Solution.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package Array.No_219_Contains_Duplicate_II;
import java.util.HashMap;
/**
* FileName: Solution
* Author: mac
* Date: 2019-02-18 10:08
* Description: Contains Duplicate II
* <p>
* Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
* <p>
* Example 1:
* <p>
* Input: nums = [1,2,3,1], k = 3
* Output: true
* Example 2:
* <p>
* Input: nums = [1,0,1,1], k = 1
* Output: true
* Example 3:
* <p>
* Input: nums = [1,2,3,1,2,3], k = 2
* Output: false
*/
public class Solution {
//时间换空间
public boolean containsNearbyDuplicate(int[] nums, int k) {
if (k < 0) return false;
HashMap<Integer, Integer> container = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (container.containsKey(nums[i])) {
if (i - container.get(nums[i]) <= k)
return true;
}
container.put(nums[i], i);
}
return false;
}
//空间底 耗时大
public boolean containsNearbyDuplicate1(int[] nums, int k) {
if (k < 0) return false;
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] == nums[j]){
if (j - i <= k)
return true;
}
}
}
return false;
}
}