Skip to content

Commit b17ac2f

Browse files
committed
feat: add solutions to lc problem: No.0973
No.0973.K Closest Points to Origin
1 parent 0968be1 commit b17ac2f

File tree

6 files changed

+102
-102
lines changed

6 files changed

+102
-102
lines changed

solution/0900-0999/0973.K Closest Points to Origin/README.md

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -49,58 +49,65 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
**方法一:自定义排序**
53+
54+
我们将所有点按照与原点的距离从小到大排序,然后取前 $k$ 个点即可。
55+
56+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 `points` 的长度。
57+
5258
<!-- tabs:start -->
5359

5460
### **Python3**
5561

5662
<!-- 这里可写当前语言的特殊实现逻辑 -->
5763

5864
```python
59-
65+
class Solution:
66+
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
67+
points.sort(key=lambda p: p[0] * p[0] + p[1] * p[1])
68+
return points[:k]
6069
```
6170

6271
### **Java**
6372

6473
<!-- 这里可写当前语言的特殊实现逻辑 -->
6574

6675
```java
67-
import java.util.*;
76+
class Solution {
77+
public int[][] kClosest(int[][] points, int k) {
78+
Arrays.sort(points, (a, b) -> {
79+
int d1 = a[0] * a[0] + a[1] * a[1];
80+
int d2 = b[0] * b[0] + b[1] * b[1];
81+
return d1 - d2;
82+
});
83+
return Arrays.copyOfRange(points, 0, k);
84+
}
85+
}
86+
```
6887

69-
/**
70-
* @author Furaha Damien
71-
*/
88+
### **C++**
7289

90+
```cpp
7391
class Solution {
74-
75-
// Helper inner class
76-
public class Point {
77-
int x;
78-
int y;
79-
int distance;
80-
81-
public Point(int x, int y, int distance) {
82-
this.x = x;
83-
this.y = y;
84-
this.distance = distance;
85-
}
92+
public:
93+
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
94+
sort(points.begin(), points.end(), [](const vector<int>& a, const vector<int>& b) {
95+
return a[0] * a[0] + a[1] * a[1] < b[0] * b[0] + b[1] * b[1];
96+
});
97+
return vector<vector<int>>(points.begin(), points.begin() + k);
8698
}
99+
};
100+
```
87101
88-
public int[][] kClosest(int[][] points, int K) {
89-
90-
PriorityQueue<Point> que = new PriorityQueue<Point>((a, b) -> (a.distance - b.distance));
91-
int[][] res = new int[K][2];
92-
93-
for (int[] temp : points) {
94-
int dist = (temp[0] * temp[0] + temp[1] * temp[1]);
95-
que.offer(new Point(temp[0], temp[1], dist));
96-
}
97-
for (int i = 0; i < K; i++) {
98-
Point curr = que.poll();
99-
res[i][0] = curr.x;
100-
res[i][1] = curr.y;
101-
}
102-
return res;
103-
}
102+
### **Go**
103+
104+
```go
105+
func kClosest(points [][]int, k int) [][]int {
106+
sort.Slice(points, func(i, j int) bool {
107+
a, b := points[i], points[j]
108+
return a[0]*a[0]+a[1]*a[1] < b[0]*b[0]+b[1]*b[1]
109+
})
110+
return points[:k]
104111
}
105112
```
106113

solution/0900-0999/0973.K Closest Points to Origin/README_EN.md

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,49 +46,50 @@ We only want the closest k = 1 points from the origin, so the answer is just [[-
4646
### **Python3**
4747

4848
```python
49-
49+
class Solution:
50+
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
51+
points.sort(key=lambda p: p[0] * p[0] + p[1] * p[1])
52+
return points[:k]
5053
```
5154

5255
### **Java**
5356

5457
```java
55-
import java.util.*;
58+
class Solution {
59+
public int[][] kClosest(int[][] points, int k) {
60+
Arrays.sort(points, (a, b) -> {
61+
int d1 = a[0] * a[0] + a[1] * a[1];
62+
int d2 = b[0] * b[0] + b[1] * b[1];
63+
return d1 - d2;
64+
});
65+
return Arrays.copyOfRange(points, 0, k);
66+
}
67+
}
68+
```
5669

57-
/**
58-
* @author Furaha Damien
59-
*/
70+
### **C++**
6071

72+
```cpp
6173
class Solution {
62-
63-
// Helper inner class
64-
public class Point {
65-
int x;
66-
int y;
67-
int distance;
68-
69-
public Point(int x, int y, int distance) {
70-
this.x = x;
71-
this.y = y;
72-
this.distance = distance;
73-
}
74+
public:
75+
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
76+
sort(points.begin(), points.end(), [](const vector<int>& a, const vector<int>& b) {
77+
return a[0] * a[0] + a[1] * a[1] < b[0] * b[0] + b[1] * b[1];
78+
});
79+
return vector<vector<int>>(points.begin(), points.begin() + k);
7480
}
81+
};
82+
```
7583
76-
public int[][] kClosest(int[][] points, int K) {
77-
78-
PriorityQueue<Point> que = new PriorityQueue<Point>((a, b) -> (a.distance - b.distance));
79-
int[][] res = new int[K][2];
80-
81-
for (int[] temp : points) {
82-
int dist = (temp[0] * temp[0] + temp[1] * temp[1]);
83-
que.offer(new Point(temp[0], temp[1], dist));
84-
}
85-
for (int i = 0; i < K; i++) {
86-
Point curr = que.poll();
87-
res[i][0] = curr.x;
88-
res[i][1] = curr.y;
89-
}
90-
return res;
91-
}
84+
### **Go**
85+
86+
```go
87+
func kClosest(points [][]int, k int) [][]int {
88+
sort.Slice(points, func(i, j int) bool {
89+
a, b := points[i], points[j]
90+
return a[0]*a[0]+a[1]*a[1] < b[0]*b[0]+b[1]*b[1]
91+
})
92+
return points[:k]
9293
}
9394
```
9495

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
4+
sort(points.begin(), points.end(), [](const vector<int>& a, const vector<int>& b) {
5+
return a[0] * a[0] + a[1] * a[1] < b[0] * b[0] + b[1] * b[1];
6+
});
7+
return vector<vector<int>>(points.begin(), points.begin() + k);
8+
}
9+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func kClosest(points [][]int, k int) [][]int {
2+
sort.Slice(points, func(i, j int) bool {
3+
a, b := points[i], points[j]
4+
return a[0]*a[0]+a[1]*a[1] < b[0]*b[0]+b[1]*b[1]
5+
})
6+
return points[:k]
7+
}
Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,10 @@
1-
import java.util.*;
2-
3-
/**
4-
* @author Furaha Damien
5-
*/
6-
71
class Solution {
8-
9-
// Helper inner class
10-
public class Point {
11-
int x;
12-
int y;
13-
int distance;
14-
15-
public Point(int x, int y, int distance) {
16-
this.x = x;
17-
this.y = y;
18-
this.distance = distance;
19-
}
2+
public int[][] kClosest(int[][] points, int k) {
3+
Arrays.sort(points, (a, b) -> {
4+
int d1 = a[0] * a[0] + a[1] * a[1];
5+
int d2 = b[0] * b[0] + b[1] * b[1];
6+
return d1 - d2;
7+
});
8+
return Arrays.copyOfRange(points, 0, k);
209
}
21-
22-
public int[][] kClosest(int[][] points, int K) {
23-
24-
PriorityQueue<Point> que = new PriorityQueue<Point>((a, b) -> (a.distance - b.distance));
25-
int[][] res = new int[K][2];
26-
27-
for (int[] temp : points) {
28-
int dist = (temp[0] * temp[0] + temp[1] * temp[1]);
29-
que.offer(new Point(temp[0], temp[1], dist));
30-
}
31-
for (int i = 0; i < K; i++) {
32-
Point curr = que.poll();
33-
res[i][0] = curr.x;
34-
res[i][1] = curr.y;
35-
}
36-
return res;
37-
}
38-
}
10+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
3+
points.sort(key=lambda p: p[0] * p[0] + p[1] * p[1])
4+
return points[:k]

0 commit comments

Comments
 (0)