-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
38 lines (32 loc) · 1.24 KB
/
Solution.java
File metadata and controls
38 lines (32 loc) · 1.24 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
35
36
37
38
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
if (nums == null)
return false;
int[] numsBak = Arrays.copyOf(nums, nums.length);
Arrays.sort(numsBak);
for (int i = 1; i < numsBak.length; i++) {
if (numsBak[i - 1] == numsBak[i]) {
int firstIdx = -1;
int secondIdx = -1;
for (int idx = 0; idx < nums.length; idx++) {
if (nums[idx] == numsBak[i]) {
if (firstIdx == -1) {
firstIdx = idx;
} else if (secondIdx == -1) {
secondIdx = idx;
}
if (firstIdx != -1 && secondIdx != -1) {
if (secondIdx - firstIdx <= k) {
return true;
} else {
firstIdx = secondIdx;
secondIdx = -1;
}
}
}
}
}
}
return false;
}
}