方法:暴力求解
class Solution {
public List<Integer> findKDistantIndices(int[] nums, int key, int k) {
List<Integer> res = new ArrayList<>();
List<Integer> pos = new ArrayList<>();
int n = nums.length;
//找到数组中等于key的数位置
for (int i = 0; i < n; i++) {
if (nums[i] == key) {
pos.add(i);
}
}
for (int i = 0; i < n; i++) {
for (int j : pos) {
if (Math.abs(i - j) <= k) {
res.add(i);
break;
}
}
}
return res;
}
}