|
| 1 | +### 题目描述 |
| 2 | + |
| 3 | +这是 LeetCode 上的 **[658. 找到 K 个最接近的元素](https://leetcode.cn/problems/find-k-closest-elements/solution/by-ac_oier-8xh5/)** ,难度为 **中等**。 |
| 4 | + |
| 5 | +Tag : 「二分」、「双指针」 |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +给定一个 排序好 的数组 `arr`,两个整数 `k` 和 `x` ,从数组中找到最靠近 `x`(两数之差最小)的 `k` 个数。返回的结果必须要是按升序排好的。 |
| 10 | + |
| 11 | +整数 `a` 比整数 `b` 更接近 `x` 需要满足: |
| 12 | + |
| 13 | +* `|a - x| < |b - x|` 或者 |
| 14 | +* `|a - x| == |b - x|` 且 `a < b` |
| 15 | + |
| 16 | +示例 1: |
| 17 | +``` |
| 18 | +输入:arr = [1,2,3,4,5], k = 4, x = 3 |
| 19 | +
|
| 20 | +输出:[1,2,3,4] |
| 21 | +``` |
| 22 | +示例 2: |
| 23 | +``` |
| 24 | +输入:arr = [1,2,3,4,5], k = 4, x = -1 |
| 25 | +
|
| 26 | +输出:[1,2,3,4] |
| 27 | +``` |
| 28 | + |
| 29 | +提示: |
| 30 | +* $1 <= k <= arr.length$ |
| 31 | +* $1 <= arr.length <= 10^4$ |
| 32 | +* `arr` 按升序排列 |
| 33 | +* $-10^4 <= arr[i], x <= 10^4$ |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +### 二分 + 双指针 |
| 38 | + |
| 39 | +容易想到先通过「二分」找到与 `x` 差值最小的位置 `idx`,然后从 `idx` 开始使用「双指针」往两边进行拓展(初始化左端点 $i = idx - 1$,右端点 $j = idx + 1$),含义为 $[i + 1, j - 1]$ 范围内子数组为候选区间,不断根据两边界与 `x` 的差值关系进行扩充,直到候选区间包含 $k$ 个数。 |
| 40 | + |
| 41 | +Java 代码: |
| 42 | +```Java |
| 43 | +class Solution { |
| 44 | + public List<Integer> findClosestElements(int[] arr, int k, int x) { |
| 45 | + int n = arr.length, l = 0, r = n - 1; |
| 46 | + while (l < r) { |
| 47 | + int mid = l + r + 1 >> 1; |
| 48 | + if (arr[mid] <= x) l = mid; |
| 49 | + else r = mid - 1; |
| 50 | + } |
| 51 | + r = r + 1 < n && Math.abs(arr[r + 1] - x) < Math.abs(arr[r] - x) ? r + 1 : r; |
| 52 | + int i = r - 1, j = r + 1; |
| 53 | + while (j - i - 1 < k) { |
| 54 | + if (i >= 0 && j < n) { |
| 55 | + if (Math.abs(arr[j] - x) < Math.abs(arr[i] - x)) j++; |
| 56 | + else i--; |
| 57 | + } else if (i >= 0) { |
| 58 | + i--; |
| 59 | + } else { |
| 60 | + j++; |
| 61 | + } |
| 62 | + } |
| 63 | + List<Integer> ans = new ArrayList<>(); |
| 64 | + for (int p = i + 1; p <= j - 1; p++) ans.add(arr[p]); |
| 65 | + return ans; |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | +TypeScript 代码: |
| 70 | +```TypeScript |
| 71 | +function findClosestElements(arr: number[], k: number, x: number): number[] { |
| 72 | + let n = arr.length, l = 0, r = n - 1 |
| 73 | + while (l < r) { |
| 74 | + const mid = l + r + 1 >> 1 |
| 75 | + if (arr[mid] <= x) l = mid |
| 76 | + else r = mid - 1 |
| 77 | + } |
| 78 | + r = r + 1 < n && Math.abs(arr[r + 1] - x) < Math.abs(arr[r] - x) ? r + 1 : r |
| 79 | + let i = r - 1, j = r + 1 |
| 80 | + while (j - i - 1 < k) { |
| 81 | + if (i >= 0 && j < n) { |
| 82 | + if (Math.abs(arr[j] - x) < Math.abs(arr[i] - x)) j++ |
| 83 | + else i-- |
| 84 | + } else if (i >= 0) { |
| 85 | + i-- |
| 86 | + } else { |
| 87 | + j++ |
| 88 | + } |
| 89 | + } |
| 90 | + return arr.slice(i + 1, j); |
| 91 | +}; |
| 92 | +``` |
| 93 | +* 时间复杂度:查找分割点复杂度为 $O(\log{n})$;从分割点往两边拓展复杂度为 $O(k)$。整体复杂度为 $O(\log{n} + k)$ |
| 94 | +* 空间复杂度:$O(1)$ |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +### 最后 |
| 99 | + |
| 100 | +这是我们「刷穿 LeetCode」系列文章的第 `No.658` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 |
| 101 | + |
| 102 | +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 |
| 103 | + |
| 104 | +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 |
| 105 | + |
| 106 | +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 |
| 107 | + |
0 commit comments