forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2200.java
29 lines (27 loc) · 900 Bytes
/
_2200.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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class _2200 {
public static class Solution1 {
public List<Integer> findKDistantIndices(int[] nums, int key, int k) {
List<Integer> ans = new ArrayList<>();
List<Integer> keyIndices = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
if (nums[i] == key) {
keyIndices.add(i);
}
}
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < keyIndices.size(); j++) {
if (Math.abs(i - keyIndices.get(j)) <= k) {
ans.add(i);
break;
}
}
}
Collections.sort(ans);
return ans;
}
}
}