LeetCode Everyday - 找出数组中的所有 K 近邻下标

82 阅读1分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第17天,点击查看活动详情

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

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

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

示例1:

输入:nums = [3,4,9,1,3,9,5], key = 9, k = 1
输出:[1,2,3,4,5,6]
解释:因此,nums[2] == key 且 nums[5] == key 。
- 对下标 0 ,|0 - 2| > k 且 |0 - 5| > k ,所以不存在 j 使得 |0 - j| <= k 且 nums[j] == key 。所以 0 不是一个 K 近邻下标。
- 对下标 1 ,|1 - 2| <= k 且 nums[2] == key ,所以 1 是一个 K 近邻下标。
- 对下标 2 ,|2 - 2| <= k 且 nums[2] == key ,所以 2 是一个 K 近邻下标。
- 对下标 3 ,|3 - 2| <= k 且 nums[2] == key ,所以 3 是一个 K 近邻下标。
- 对下标 4 ,|4 - 5| <= k 且 nums[5] == key ,所以 4 是一个 K 近邻下标。
- 对下标 5 ,|5 - 5| <= k 且 nums[5] == key ,所以 5 是一个 K 近邻下标。
- 对下标 6 ,|6 - 5| <= k 且 nums[5] == key ,所以 6 是一个 K 近邻下标。
因此,按递增顺序返回 [1,2,3,4,5,6] 。 

示例2:

输入:nums = [2,2,2,2,2], key = 2, k = 2
输出:[0,1,2,3,4]
解释:对 nums 的所有下标 i ,总存在某个下标 j 使得 |i - j| <= k 且 nums[j] == key ,所以每个下标都是一个 K 近邻下标。 
因此,返回 [0,1,2,3,4] 。

提示:

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 1000
  • key 是数组 nums 中的一个整数
  • 1 <= k <= nums.length

解题思路:

  1. 遍历找到 key 的 index 的 list
  2. 再次遍历nums,将下标i和最近的key Index 比较 i 和 keyIndex 的差值是否满足小于等于k

我的答案:

/**
 * @param {number[]} nums
 * @param {number} key
 * @param {number} k
 * @return {number[]}
 */
var findKDistantIndices = function(nums, key, k) {
    let keyIndexList = []
    nums.forEach((num, index) => {
        if (num === key) keyIndexList.push(index)
    })
    
    let result = []
    let prevKeyIndex = -1
    for (let i = 0; i < keyIndexList.length; i++) {
        let nowKeyIndex = keyIndexList[i]
        for (let t = prevKeyIndex + 1; t <= nowKeyIndex; t++) {
            if (nowKeyIndex - t <= k) {
                result.push(t)
                continue
            } else if (prevKeyIndex !== -1 && t - prevKeyIndex <= k) {
                result.push(t)
            }
        }
        prevKeyIndex = nowKeyIndex
    }
    for (let s = prevKeyIndex + 1; s < nums.length; s++) {
        console.log('s', s)
        if (prevKeyIndex !== -1 && s - prevKeyIndex <= k) result.push(s)
    }

    return result
};

最后

如果有更好的解法或者思路, 欢迎在评论区和我交流~ ღ( ´・ᴗ・` )