Skip to content

Commit 4aa944b

Browse files
committed
feat: add solutions to lc problem: No.1779
No.1779.Find Nearest Point That Has the Same X or Y Coordinate
1 parent 38b5c48 commit 4aa944b

File tree

6 files changed

+210
-2
lines changed

6 files changed

+210
-2
lines changed

solution/1700-1799/1779.Find Nearest Point That Has the Same X or Y Coordinate/README.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,98 @@
4949

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

52+
**方法一:直接遍历**
53+
54+
直接遍历 `points` 数组,对于 `points[i]`,如果 `points[i][0] == x` 或者 `points[i][1] == y`,则说明 `points[i]` 是有效点,计算曼哈顿距离,更新最小距离和最小距离的下标。
55+
56+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为 `points` 数组的长度。
57+
5258
<!-- tabs:start -->
5359

5460
### **Python3**
5561

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

5864
```python
59-
65+
class Solution:
66+
def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:
67+
ans, mi = -1, inf
68+
for i, (a, b) in enumerate(points):
69+
if a == x or b == y:
70+
d = abs(a - x) + abs(b - y)
71+
if mi > d:
72+
ans, mi = i, d
73+
return ans
6074
```
6175

6276
### **Java**
6377

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

6680
```java
81+
class Solution {
82+
public int nearestValidPoint(int x, int y, int[][] points) {
83+
int ans = -1, mi = 1000000;
84+
for (int i = 0; i < points.length; ++i) {
85+
int a = points[i][0], b = points[i][1];
86+
if (a == x || b == y) {
87+
int d = Math.abs(a - x) + Math.abs(b - y);
88+
if (d < mi) {
89+
mi = d;
90+
ans = i;
91+
}
92+
}
93+
}
94+
return ans;
95+
}
96+
}
97+
```
6798

99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int nearestValidPoint(int x, int y, vector<vector<int>>& points) {
105+
int ans = -1, mi = 1e6;
106+
for (int i = 0; i < points.size(); ++i) {
107+
int a = points[i][0], b = points[i][1];
108+
if (a == x || b == y) {
109+
int d = abs(a - x) + abs(b - y);
110+
if (d < mi) {
111+
mi = d;
112+
ans = i;
113+
}
114+
}
115+
}
116+
return ans;
117+
}
118+
};
119+
```
120+
121+
### **Go**
122+
123+
```go
124+
func nearestValidPoint(x int, y int, points [][]int) int {
125+
ans, mi := -1, 1000000
126+
for i, p := range points {
127+
a, b := p[0], p[1]
128+
if a == x || b == y {
129+
d := abs(a-x) + abs(b-y)
130+
if d < mi {
131+
ans, mi = i, d
132+
}
133+
}
134+
}
135+
return ans
136+
}
137+
138+
func abs(x int) int {
139+
if x < 0 {
140+
return -x
141+
}
142+
return x
143+
}
68144
```
69145

70146
### **TypeScript**

solution/1700-1799/1779.Find Nearest Point That Has the Same X or Y Coordinate/README_EN.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,83 @@
4848
### **Python3**
4949

5050
```python
51-
51+
class Solution:
52+
def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:
53+
ans, mi = -1, inf
54+
for i, (a, b) in enumerate(points):
55+
if a == x or b == y:
56+
d = abs(a - x) + abs(b - y)
57+
if mi > d:
58+
ans, mi = i, d
59+
return ans
5260
```
5361

5462
### **Java**
5563

5664
```java
65+
class Solution {
66+
public int nearestValidPoint(int x, int y, int[][] points) {
67+
int ans = -1, mi = 1000000;
68+
for (int i = 0; i < points.length; ++i) {
69+
int a = points[i][0], b = points[i][1];
70+
if (a == x || b == y) {
71+
int d = Math.abs(a - x) + Math.abs(b - y);
72+
if (d < mi) {
73+
mi = d;
74+
ans = i;
75+
}
76+
}
77+
}
78+
return ans;
79+
}
80+
}
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
int nearestValidPoint(int x, int y, vector<vector<int>>& points) {
89+
int ans = -1, mi = 1e6;
90+
for (int i = 0; i < points.size(); ++i) {
91+
int a = points[i][0], b = points[i][1];
92+
if (a == x || b == y) {
93+
int d = abs(a - x) + abs(b - y);
94+
if (d < mi) {
95+
mi = d;
96+
ans = i;
97+
}
98+
}
99+
}
100+
return ans;
101+
}
102+
};
103+
```
57104
105+
### **Go**
106+
107+
```go
108+
func nearestValidPoint(x int, y int, points [][]int) int {
109+
ans, mi := -1, 1000000
110+
for i, p := range points {
111+
a, b := p[0], p[1]
112+
if a == x || b == y {
113+
d := abs(a-x) + abs(b-y)
114+
if d < mi {
115+
ans, mi = i, d
116+
}
117+
}
118+
}
119+
return ans
120+
}
121+
122+
func abs(x int) int {
123+
if x < 0 {
124+
return -x
125+
}
126+
return x
127+
}
58128
```
59129

60130
### **TypeScript**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int nearestValidPoint(int x, int y, vector<vector<int>>& points) {
4+
int ans = -1, mi = 1e6;
5+
for (int i = 0; i < points.size(); ++i) {
6+
int a = points[i][0], b = points[i][1];
7+
if (a == x || b == y) {
8+
int d = abs(a - x) + abs(b - y);
9+
if (d < mi) {
10+
mi = d;
11+
ans = i;
12+
}
13+
}
14+
}
15+
return ans;
16+
}
17+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func nearestValidPoint(x int, y int, points [][]int) int {
2+
ans, mi := -1, 1000000
3+
for i, p := range points {
4+
a, b := p[0], p[1]
5+
if a == x || b == y {
6+
d := abs(a-x) + abs(b-y)
7+
if d < mi {
8+
ans, mi = i, d
9+
}
10+
}
11+
}
12+
return ans
13+
}
14+
15+
func abs(x int) int {
16+
if x < 0 {
17+
return -x
18+
}
19+
return x
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int nearestValidPoint(int x, int y, int[][] points) {
3+
int ans = -1, mi = 1000000;
4+
for (int i = 0; i < points.length; ++i) {
5+
int a = points[i][0], b = points[i][1];
6+
if (a == x || b == y) {
7+
int d = Math.abs(a - x) + Math.abs(b - y);
8+
if (d < mi) {
9+
mi = d;
10+
ans = i;
11+
}
12+
}
13+
}
14+
return ans;
15+
}
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:
3+
ans, mi = -1, inf
4+
for i, (a, b) in enumerate(points):
5+
if a == x or b == y:
6+
d = abs(a - x) + abs(b - y)
7+
if mi > d:
8+
ans, mi = i, d
9+
return ans

0 commit comments

Comments
 (0)