Skip to content

Commit c2d221f

Browse files
authored
feat: add solutions to lc problem: No.0356 (doocs#1614)
No.0356.Line Reflection
1 parent d484bea commit c2d221f

File tree

6 files changed

+212
-41
lines changed

6 files changed

+212
-41
lines changed

solution/0300-0399/0356.Line Reflection/README.md

+78-15
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48-
先找出所有点中的最小、最大的 x 坐标 `minX``maxX`。若存在满足条件的直线,则直线 `x = (minX + maxX) / 2`。(或者说:`s = minX + maxX`)
48+
**方法一:哈希表**
4949

50-
遍历每个点 `point(x, y)`,若 `(s - x, y)` 不在点集里,说明不满足条件,直接返回 false。遍历结束返回 true。
50+
我们先找出所有点中的最小、最大的 $x$ 坐标 $minX$ 和 $maxX$。若存在满足条件的直线,则直线 $x = (minX + maxX) / 2$,或者说 $s = minX + maxX$。
51+
52+
接下来,我们遍历每个点 $(x, y),若 $(s - x, y)$ 不在点集里,说明不满足条件,直接返回 `false`。遍历结束返回 `true`
53+
54+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $points$ 的长度。
5155

5256
<!-- tabs:start -->
5357

@@ -65,10 +69,7 @@ class Solution:
6569
max_x = max(max_x, x)
6670
point_set.add((x, y))
6771
s = min_x + max_x
68-
for x, y in points:
69-
if (s - x, y) not in point_set:
70-
return False
71-
return True
72+
return all((s - x, y) in point_set for x, y in points)
7273
```
7374

7475
### **Java**
@@ -78,21 +79,83 @@ class Solution:
7879
```java
7980
class Solution {
8081
public boolean isReflected(int[][] points) {
81-
int minX = Integer.MAX_VALUE, maxX = Integer.MIN_VALUE;
82-
Set<String> pointSet = new HashSet<>();
83-
for (int[] point : points) {
84-
minX = Math.min(minX, point[0]);
85-
maxX = Math.max(maxX, point[0]);
86-
pointSet.add(point[0] + "." + point[1]);
82+
final int inf = 1 << 30;
83+
int minX = inf, maxX = -inf;
84+
Set<List<Integer>> pointSet = new HashSet<>();
85+
for (int[] p : points) {
86+
minX = Math.min(minX, p[0]);
87+
maxX = Math.max(maxX, p[0]);
88+
pointSet.add(List.of(p[0], p[1]));
89+
}
90+
int s = minX + maxX;
91+
for (int[] p : points) {
92+
if (!pointSet.contains(List.of(s - p[0], p[1]))) {
93+
return false;
94+
}
95+
}
96+
return true;
97+
}
98+
}
99+
```
100+
101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
bool isReflected(vector<vector<int>>& points) {
107+
const int inf = 1 << 30;
108+
int minX = inf, maxX = -inf;
109+
set<pair<int, int>> pointSet;
110+
for (auto& p : points) {
111+
minX = min(minX, p[0]);
112+
maxX = max(maxX, p[0]);
113+
pointSet.insert({p[0], p[1]});
87114
}
88-
long s = minX + maxX;
89-
for (int[] point : points) {
90-
if (!pointSet.contains((s - point[0]) + "." + point[1])) {
115+
int s = minX + maxX;
116+
for (auto& p : points) {
117+
if (!pointSet.count({s - p[0], p[1]})) {
91118
return false;
92119
}
93120
}
94121
return true;
95122
}
123+
};
124+
```
125+
126+
### **Go**
127+
128+
```go
129+
func isReflected(points [][]int) bool {
130+
const inf = 1 << 30
131+
minX, maxX := inf, -inf
132+
pointSet := map[[2]int]bool{}
133+
for _, p := range points {
134+
minX = min(minX, p[0])
135+
maxX = max(maxX, p[0])
136+
pointSet[[2]int{p[0], p[1]}] = true
137+
}
138+
s := minX + maxX
139+
for _, p := range points {
140+
if !pointSet[[2]int{s - p[0], p[1]}] {
141+
return false
142+
}
143+
}
144+
return true
145+
}
146+
147+
func min(a, b int) int {
148+
if a < b {
149+
return a
150+
}
151+
return b
152+
}
153+
154+
func max(a, b int) int {
155+
if a > b {
156+
return a
157+
}
158+
return b
96159
}
97160
```
98161

solution/0300-0399/0356.Line Reflection/README_EN.md

+72-13
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,91 @@ class Solution:
5555
max_x = max(max_x, x)
5656
point_set.add((x, y))
5757
s = min_x + max_x
58-
for x, y in points:
59-
if (s - x, y) not in point_set:
60-
return False
61-
return True
58+
return all((s - x, y) in point_set for x, y in points)
6259
```
6360

6461
### **Java**
6562

6663
```java
6764
class Solution {
6865
public boolean isReflected(int[][] points) {
69-
int minX = Integer.MAX_VALUE, maxX = Integer.MIN_VALUE;
70-
Set<String> pointSet = new HashSet<>();
71-
for (int[] point : points) {
72-
minX = Math.min(minX, point[0]);
73-
maxX = Math.max(maxX, point[0]);
74-
pointSet.add(point[0] + "." + point[1]);
66+
final int inf = 1 << 30;
67+
int minX = inf, maxX = -inf;
68+
Set<List<Integer>> pointSet = new HashSet<>();
69+
for (int[] p : points) {
70+
minX = Math.min(minX, p[0]);
71+
maxX = Math.max(maxX, p[0]);
72+
pointSet.add(List.of(p[0], p[1]));
73+
}
74+
int s = minX + maxX;
75+
for (int[] p : points) {
76+
if (!pointSet.contains(List.of(s - p[0], p[1]))) {
77+
return false;
78+
}
79+
}
80+
return true;
81+
}
82+
}
83+
```
84+
85+
### **C++**
86+
87+
```cpp
88+
class Solution {
89+
public:
90+
bool isReflected(vector<vector<int>>& points) {
91+
const int inf = 1 << 30;
92+
int minX = inf, maxX = -inf;
93+
set<pair<int, int>> pointSet;
94+
for (auto& p : points) {
95+
minX = min(minX, p[0]);
96+
maxX = max(maxX, p[0]);
97+
pointSet.insert({p[0], p[1]});
7598
}
76-
long s = minX + maxX;
77-
for (int[] point : points) {
78-
if (!pointSet.contains((s - point[0]) + "." + point[1])) {
99+
int s = minX + maxX;
100+
for (auto& p : points) {
101+
if (!pointSet.count({s - p[0], p[1]})) {
79102
return false;
80103
}
81104
}
82105
return true;
83106
}
107+
};
108+
```
109+
110+
### **Go**
111+
112+
```go
113+
func isReflected(points [][]int) bool {
114+
const inf = 1 << 30
115+
minX, maxX := inf, -inf
116+
pointSet := map[[2]int]bool{}
117+
for _, p := range points {
118+
minX = min(minX, p[0])
119+
maxX = max(maxX, p[0])
120+
pointSet[[2]int{p[0], p[1]}] = true
121+
}
122+
s := minX + maxX
123+
for _, p := range points {
124+
if !pointSet[[2]int{s - p[0], p[1]}] {
125+
return false
126+
}
127+
}
128+
return true
129+
}
130+
131+
func min(a, b int) int {
132+
if a < b {
133+
return a
134+
}
135+
return b
136+
}
137+
138+
func max(a, b int) int {
139+
if a > b {
140+
return a
141+
}
142+
return b
84143
}
85144
```
86145

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
bool isReflected(vector<vector<int>>& points) {
4+
const int inf = 1 << 30;
5+
int minX = inf, maxX = -inf;
6+
set<pair<int, int>> pointSet;
7+
for (auto& p : points) {
8+
minX = min(minX, p[0]);
9+
maxX = max(maxX, p[0]);
10+
pointSet.insert({p[0], p[1]});
11+
}
12+
int s = minX + maxX;
13+
for (auto& p : points) {
14+
if (!pointSet.count({s - p[0], p[1]})) {
15+
return false;
16+
}
17+
}
18+
return true;
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
func isReflected(points [][]int) bool {
2+
const inf = 1 << 30
3+
minX, maxX := inf, -inf
4+
pointSet := map[[2]int]bool{}
5+
for _, p := range points {
6+
minX = min(minX, p[0])
7+
maxX = max(maxX, p[0])
8+
pointSet[[2]int{p[0], p[1]}] = true
9+
}
10+
s := minX + maxX
11+
for _, p := range points {
12+
if !pointSet[[2]int{s - p[0], p[1]}] {
13+
return false
14+
}
15+
}
16+
return true
17+
}
18+
19+
func min(a, b int) int {
20+
if a < b {
21+
return a
22+
}
23+
return b
24+
}
25+
26+
func max(a, b int) int {
27+
if a > b {
28+
return a
29+
}
30+
return b
31+
}

solution/0300-0399/0356.Line Reflection/Solution.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
class Solution {
22
public boolean isReflected(int[][] points) {
3-
int minX = Integer.MAX_VALUE, maxX = Integer.MIN_VALUE;
4-
Set<String> pointSet = new HashSet<>();
5-
for (int[] point : points) {
6-
minX = Math.min(minX, point[0]);
7-
maxX = Math.max(maxX, point[0]);
8-
pointSet.add(point[0] + "." + point[1]);
3+
final int inf = 1 << 30;
4+
int minX = inf, maxX = -inf;
5+
Set<List<Integer>> pointSet = new HashSet<>();
6+
for (int[] p : points) {
7+
minX = Math.min(minX, p[0]);
8+
maxX = Math.max(maxX, p[0]);
9+
pointSet.add(List.of(p[0], p[1]));
910
}
10-
long s = minX + maxX;
11-
for (int[] point : points) {
12-
if (!pointSet.contains((s - point[0]) + "." + point[1])) {
11+
int s = minX + maxX;
12+
for (int[] p : points) {
13+
if (!pointSet.contains(List.of(s - p[0], p[1]))) {
1314
return false;
1415
}
1516
}

solution/0300-0399/0356.Line Reflection/Solution.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,4 @@ def isReflected(self, points: List[List[int]]) -> bool:
77
max_x = max(max_x, x)
88
point_set.add((x, y))
99
s = min_x + max_x
10-
for x, y in points:
11-
if (s - x, y) not in point_set:
12-
return False
13-
return True
10+
return all((s - x, y) in point_set for x, y in points)

0 commit comments

Comments
 (0)