|
1 | 1 | [#0973-k-closest-points-to-origin] |
2 | | -= 973. K Closest Points to Origin |
| 2 | += 973. 最接近原点的 K 个点 |
3 | 3 |
|
4 | | -{leetcode}/problems/k-closest-points-to-origin/[LeetCode - K Closest Points to Origin^] |
| 4 | +https://leetcode.cn/problems/k-closest-points-to-origin/[LeetCode - 973. 最接近原点的 K 个点^] |
5 | 5 |
|
6 | | -We have a list of `points` on the plane. Find the `K` closest points to the origin `(0, 0)`. |
| 6 | +给定一个数组 `points`,其中 `points[i] = [x~i~, y~i~]` 表示 *X-Y* 平面上的一个点,并且是一个整数 `k`,返回离原点 `(0, 0)` 最近的 `k` 个点。 |
7 | 7 |
|
8 | | -(Here, the distance between two points on a plane is the Euclidean distance.) |
| 8 | +这里,平面上两点之间的距离是 *欧几里德距离* stem:[\sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}]。 |
9 | 9 |
|
10 | | -You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.) |
| 10 | +你可以按 *任何顺序* 返回答案。除了点坐标的顺序之外,答案 *确保* 是 *唯一* 的。 |
11 | 11 |
|
12 | | - |
| 12 | +*示例 1:* |
13 | 13 |
|
| 14 | +image::images/0973-01.jpg[{image_attr}] |
14 | 15 |
|
15 | | -*Example 1:* |
16 | | - |
17 | | -[subs="verbatim,quotes,macros"] |
18 | | ----- |
19 | | -*Input:* points = [[1,3],[-2,2]], K = 1 |
20 | | -*Output:* [[-2,2]] |
21 | | -*Explanation:* |
22 | | -The distance between (1, 3) and the origin is sqrt(10). |
23 | | -The distance between (-2, 2) and the origin is sqrt(8). |
24 | | -Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin. |
25 | | -We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]]. |
26 | | ----- |
| 16 | +.... |
| 17 | +输入:points = [[1,3],[-2,2]], k = 1 |
| 18 | +输出:[[-2,2]] |
| 19 | +解释: |
| 20 | +(1, 3) 和原点之间的距离为 sqrt(10), |
| 21 | +(-2, 2) 和原点之间的距离为 sqrt(8), |
| 22 | +由于 sqrt(8) < sqrt(10),(-2, 2) 离原点更近。 |
| 23 | +我们只需要距离原点最近的 K = 1 个点,所以答案就是 [[-2,2]]。 |
| 24 | +.... |
27 | 25 |
|
| 26 | +*示例 2:* |
28 | 27 |
|
29 | | -*Example 2:* |
30 | | - |
31 | | -[subs="verbatim,quotes,macros"] |
32 | | ----- |
33 | | -*Input:* points = [[3,3],[5,-1],[-2,4]], K = 2 |
34 | | -*Output:* [[3,3],[-2,4]] |
35 | | -(The answer [[-2,4],[3,3]] would also be accepted.) |
36 | | ----- |
| 28 | +.... |
| 29 | +输入:points = [[3,3],[5,-1],[-2,4]], k = 2 |
| 30 | +输出:[[3,3],[-2,4]] |
| 31 | +(答案 [[-2,4],[3,3]] 也会被接受。) |
| 32 | +.... |
37 | 33 |
|
38 | | - |
| 34 | +*提示:* |
39 | 35 |
|
40 | | -*Note:* |
| 36 | +* `1 \<= k \<= points.length \<= 10^4^` |
| 37 | +* `-10^4^ < x~i~, y~i~ < 10^4^` |
41 | 38 |
|
42 | 39 |
|
43 | | -. `1 <= K <= points.length <= 10000` |
44 | | -. `-10000 < points[i][0] < 10000` |
45 | | -. `-10000 < points[i][1] < 10000` |
46 | | - |
47 | 40 |
|
| 41 | +== 思路分析 |
48 | 42 |
|
| 43 | +排序,堆,快速选择等多种解法均可。 |
49 | 44 |
|
| 45 | +WARNING: 快速选择写的还是漏洞百出!! |
50 | 46 |
|
51 | 47 | [[src-0973]] |
| 48 | +[tabs] |
| 49 | +==== |
| 50 | +一刷:: |
| 51 | ++ |
| 52 | +-- |
52 | 53 | [{java_src_attr}] |
53 | 54 | ---- |
54 | 55 | include::{sourcedir}/_0973_KClosestPointsToOrigin.java[tag=answer] |
55 | 56 | ---- |
| 57 | +-- |
| 58 | +
|
| 59 | +// 二刷:: |
| 60 | +// + |
| 61 | +// -- |
| 62 | +// [{java_src_attr}] |
| 63 | +// ---- |
| 64 | +// include::{sourcedir}/_0973_KClosestPointsToOrigin_2.java[tag=answer] |
| 65 | +// ---- |
| 66 | +// -- |
| 67 | +==== |
| 68 | + |
| 69 | + |
| 70 | +== 参考资料 |
| 71 | + |
| 72 | +. https://leetcode.cn/problems/k-closest-points-to-origin/solutions/478516/kuai-lai-miao-dong-topkkuai-pai-bian-xing-da-gen-d/[973. 最接近原点的 K 个点 - 快来秒懂 TopK!快排变形 & 大根堆^] |
| 73 | +. https://leetcode.cn/problems/k-closest-points-to-origin/solutions/477916/zui-jie-jin-yuan-dian-de-k-ge-dian-by-leetcode-sol/[973. 最接近原点的 K 个点 - 官方题解^] |
56 | 74 |
|
0 commit comments