[LeetCode] 2200. Find All K-Distant Indices in an Array

You are given a 0-indexed integer array nums and two integers key and k. A k-distant index is an index i of nums for which there exists at least one index j such that |i - j| <= k and nums[j] == key.

Return a list of all k-distant indices sorted in increasing order.

Example 1:
Input: nums = [3,4,9,1,3,9,5], key = 9, k = 1
Output: [1,2,3,4,5,6]
Explanation: Here, nums[2] == key and nums[5] == key.

  • For index 0, |0 - 2| > k and |0 - 5| > k, so there is no j where |0 - j| <= k and nums[j] == key. Thus, 0 is not a k-distant index.
  • For index 1, |1 - 2| <= k and nums[2] == key, so 1 is a k-distant index.
  • For index 2, |2 - 2| <= k and nums[2] == key, so 2 is a k-distant index.
  • For index 3, |3 - 2| <= k and nums[2] == key, so 3 is a k-distant index.
  • For index 4, |4 - 5| <= k and nums[5] == key, so 4 is a k-distant index.
  • For index 5, |5 - 5| <= k and nums[5] == key, so 5 is a k-distant index.
  • For index 6, |6 - 5| <= k and nums[5] == key, so 6 is a k-distant index.
    Thus, we return [1,2,3,4,5,6] which is sorted in increasing order.

Example 2:
Input: nums = [2,2,2,2,2], key = 2, k = 2
Output: [0,1,2,3,4]
Explanation: For all indices i in nums, there exists some index j such that |i - j| <= k and nums[j] == key, so every index is a k-distant index.
Hence, we return [0,1,2,3,4].

Constraints:
1 <= nums.length <= 1000
1 <= nums[i] <= 1000
key is an integer from the array nums.
1 <= k <= nums.length

找出数组中的所有 K 近邻下标。

给你一个下标从 0 开始的整数数组 nums 和两个整数 key 和 k 。K 近邻下标 是 nums 中的一个下标 i ,并满足至少存在一个下标 j 使得 |i - j| <= k 且 nums[j] == key 。

以列表形式返回按 递增顺序 排序的所有 K 近邻下标。

思路一 - 两层循环

先用一个 list 把 input 数组里所有等于 key 的下标存起来,然后遍历这个 list,对于 list 里的每一个 index,将在 [index - k, index + k] 这个范围内的所有下标都加入一个 hashset 里。再把 hashset 转换成 list 并排序后返回。

复杂度

时间O(n*k)
空间O(n)

代码

Java实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public List<Integer> findKDistantIndices(int[] nums, int key, int k) {
Set<Integer> set = new HashSet<>();
int n = nums.length;
for (int j = 0; j < n; j++) {
if (nums[j] == key) {
int start = Math.max(0, j - k);
int end = Math.min(n - 1, j + k);
for (int i = start; i <= end; i++) {
set.add(i);
}
}
}

List<Integer> res = new ArrayList<>(set);
Collections.sort(res); // 题目要求升序排列
return res;
}
}
// O(n * k)

思路二 - 双指针,一层循环

和思路一类似,还是需要一个 list 先把所有 key 的下标存储起来。接着我们用双指针的做法,一个指针 i 指向 input 数组,另一个指针 j 指向 list。具体参见代码。

复杂度

时间O(m + n)
空间O(n)

代码

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
class Solution {
public List<Integer> findKDistantIndices(int[] nums, int key, int k) {
List<Integer> indexes = new ArrayList<>();
int n = nums.length;
for (int i = 0; i < n; i++) {
int num = nums[i];
if (num == key) {
indexes.add(i);
}
}

List<Integer> res = new ArrayList<>();
int p = 0;
for (int i = 0; i < n; i++) {
while (p < indexes.size() && indexes.get(p) < i - k) {
p++;
}
if (p < indexes.size() && Math.abs(i - indexes.get(p)) <= k) {
res.add(i);
}
}
return res;
}
}
// O(m+n)

[LeetCode] 2200. Find All K-Distant Indices in an Array
https://shurui91.github.io/posts/3512283209.html
Author
Aaron Liu
Posted on
June 23, 2025
Licensed under