mirror of https://github.com/doocs/leetcode.git
feat: add solutions to lc problem: No.0356 (#1614)
No.0356.Line Reflection
This commit is contained in:
parent
d484bea7ec
commit
c2d221fd2f
|
|
@ -45,9 +45,13 @@
|
|||
|
||||
<!-- 这里可写通用的实现逻辑 -->
|
||||
|
||||
先找出所有点中的最小、最大的 x 坐标 `minX` 和 `maxX`。若存在满足条件的直线,则直线 `x = (minX + maxX) / 2`。(或者说:`s = minX + maxX`)
|
||||
**方法一:哈希表**
|
||||
|
||||
遍历每个点 `point(x, y)`,若 `(s - x, y)` 不在点集里,说明不满足条件,直接返回 false。遍历结束返回 true。
|
||||
我们先找出所有点中的最小、最大的 $x$ 坐标 $minX$ 和 $maxX$。若存在满足条件的直线,则直线 $x = (minX + maxX) / 2$,或者说 $s = minX + maxX$。
|
||||
|
||||
接下来,我们遍历每个点 $(x, y),若 $(s - x, y)$ 不在点集里,说明不满足条件,直接返回 `false`。遍历结束返回 `true`。
|
||||
|
||||
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $points$ 的长度。
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
|
|
@ -65,10 +69,7 @@ class Solution:
|
|||
max_x = max(max_x, x)
|
||||
point_set.add((x, y))
|
||||
s = min_x + max_x
|
||||
for x, y in points:
|
||||
if (s - x, y) not in point_set:
|
||||
return False
|
||||
return True
|
||||
return all((s - x, y) in point_set for x, y in points)
|
||||
```
|
||||
|
||||
### **Java**
|
||||
|
|
@ -78,16 +79,17 @@ class Solution:
|
|||
```java
|
||||
class Solution {
|
||||
public boolean isReflected(int[][] points) {
|
||||
int minX = Integer.MAX_VALUE, maxX = Integer.MIN_VALUE;
|
||||
Set<String> pointSet = new HashSet<>();
|
||||
for (int[] point : points) {
|
||||
minX = Math.min(minX, point[0]);
|
||||
maxX = Math.max(maxX, point[0]);
|
||||
pointSet.add(point[0] + "." + point[1]);
|
||||
final int inf = 1 << 30;
|
||||
int minX = inf, maxX = -inf;
|
||||
Set<List<Integer>> pointSet = new HashSet<>();
|
||||
for (int[] p : points) {
|
||||
minX = Math.min(minX, p[0]);
|
||||
maxX = Math.max(maxX, p[0]);
|
||||
pointSet.add(List.of(p[0], p[1]));
|
||||
}
|
||||
long s = minX + maxX;
|
||||
for (int[] point : points) {
|
||||
if (!pointSet.contains((s - point[0]) + "." + point[1])) {
|
||||
int s = minX + maxX;
|
||||
for (int[] p : points) {
|
||||
if (!pointSet.contains(List.of(s - p[0], p[1]))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -96,6 +98,67 @@ class Solution {
|
|||
}
|
||||
```
|
||||
|
||||
### **C++**
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
bool isReflected(vector<vector<int>>& points) {
|
||||
const int inf = 1 << 30;
|
||||
int minX = inf, maxX = -inf;
|
||||
set<pair<int, int>> pointSet;
|
||||
for (auto& p : points) {
|
||||
minX = min(minX, p[0]);
|
||||
maxX = max(maxX, p[0]);
|
||||
pointSet.insert({p[0], p[1]});
|
||||
}
|
||||
int s = minX + maxX;
|
||||
for (auto& p : points) {
|
||||
if (!pointSet.count({s - p[0], p[1]})) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### **Go**
|
||||
|
||||
```go
|
||||
func isReflected(points [][]int) bool {
|
||||
const inf = 1 << 30
|
||||
minX, maxX := inf, -inf
|
||||
pointSet := map[[2]int]bool{}
|
||||
for _, p := range points {
|
||||
minX = min(minX, p[0])
|
||||
maxX = max(maxX, p[0])
|
||||
pointSet[[2]int{p[0], p[1]}] = true
|
||||
}
|
||||
s := minX + maxX
|
||||
for _, p := range points {
|
||||
if !pointSet[[2]int{s - p[0], p[1]}] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
||||
```
|
||||
|
|
|
|||
|
|
@ -55,10 +55,7 @@ class Solution:
|
|||
max_x = max(max_x, x)
|
||||
point_set.add((x, y))
|
||||
s = min_x + max_x
|
||||
for x, y in points:
|
||||
if (s - x, y) not in point_set:
|
||||
return False
|
||||
return True
|
||||
return all((s - x, y) in point_set for x, y in points)
|
||||
```
|
||||
|
||||
### **Java**
|
||||
|
|
@ -66,16 +63,17 @@ class Solution:
|
|||
```java
|
||||
class Solution {
|
||||
public boolean isReflected(int[][] points) {
|
||||
int minX = Integer.MAX_VALUE, maxX = Integer.MIN_VALUE;
|
||||
Set<String> pointSet = new HashSet<>();
|
||||
for (int[] point : points) {
|
||||
minX = Math.min(minX, point[0]);
|
||||
maxX = Math.max(maxX, point[0]);
|
||||
pointSet.add(point[0] + "." + point[1]);
|
||||
final int inf = 1 << 30;
|
||||
int minX = inf, maxX = -inf;
|
||||
Set<List<Integer>> pointSet = new HashSet<>();
|
||||
for (int[] p : points) {
|
||||
minX = Math.min(minX, p[0]);
|
||||
maxX = Math.max(maxX, p[0]);
|
||||
pointSet.add(List.of(p[0], p[1]));
|
||||
}
|
||||
long s = minX + maxX;
|
||||
for (int[] point : points) {
|
||||
if (!pointSet.contains((s - point[0]) + "." + point[1])) {
|
||||
int s = minX + maxX;
|
||||
for (int[] p : points) {
|
||||
if (!pointSet.contains(List.of(s - p[0], p[1]))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -84,6 +82,67 @@ class Solution {
|
|||
}
|
||||
```
|
||||
|
||||
### **C++**
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
bool isReflected(vector<vector<int>>& points) {
|
||||
const int inf = 1 << 30;
|
||||
int minX = inf, maxX = -inf;
|
||||
set<pair<int, int>> pointSet;
|
||||
for (auto& p : points) {
|
||||
minX = min(minX, p[0]);
|
||||
maxX = max(maxX, p[0]);
|
||||
pointSet.insert({p[0], p[1]});
|
||||
}
|
||||
int s = minX + maxX;
|
||||
for (auto& p : points) {
|
||||
if (!pointSet.count({s - p[0], p[1]})) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### **Go**
|
||||
|
||||
```go
|
||||
func isReflected(points [][]int) bool {
|
||||
const inf = 1 << 30
|
||||
minX, maxX := inf, -inf
|
||||
pointSet := map[[2]int]bool{}
|
||||
for _, p := range points {
|
||||
minX = min(minX, p[0])
|
||||
maxX = max(maxX, p[0])
|
||||
pointSet[[2]int{p[0], p[1]}] = true
|
||||
}
|
||||
s := minX + maxX
|
||||
for _, p := range points {
|
||||
if !pointSet[[2]int{s - p[0], p[1]}] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
||||
```
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
class Solution {
|
||||
public:
|
||||
bool isReflected(vector<vector<int>>& points) {
|
||||
const int inf = 1 << 30;
|
||||
int minX = inf, maxX = -inf;
|
||||
set<pair<int, int>> pointSet;
|
||||
for (auto& p : points) {
|
||||
minX = min(minX, p[0]);
|
||||
maxX = max(maxX, p[0]);
|
||||
pointSet.insert({p[0], p[1]});
|
||||
}
|
||||
int s = minX + maxX;
|
||||
for (auto& p : points) {
|
||||
if (!pointSet.count({s - p[0], p[1]})) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
func isReflected(points [][]int) bool {
|
||||
const inf = 1 << 30
|
||||
minX, maxX := inf, -inf
|
||||
pointSet := map[[2]int]bool{}
|
||||
for _, p := range points {
|
||||
minX = min(minX, p[0])
|
||||
maxX = max(maxX, p[0])
|
||||
pointSet[[2]int{p[0], p[1]}] = true
|
||||
}
|
||||
s := minX + maxX
|
||||
for _, p := range points {
|
||||
if !pointSet[[2]int{s - p[0], p[1]}] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
|
@ -1,15 +1,16 @@
|
|||
class Solution {
|
||||
public boolean isReflected(int[][] points) {
|
||||
int minX = Integer.MAX_VALUE, maxX = Integer.MIN_VALUE;
|
||||
Set<String> pointSet = new HashSet<>();
|
||||
for (int[] point : points) {
|
||||
minX = Math.min(minX, point[0]);
|
||||
maxX = Math.max(maxX, point[0]);
|
||||
pointSet.add(point[0] + "." + point[1]);
|
||||
final int inf = 1 << 30;
|
||||
int minX = inf, maxX = -inf;
|
||||
Set<List<Integer>> pointSet = new HashSet<>();
|
||||
for (int[] p : points) {
|
||||
minX = Math.min(minX, p[0]);
|
||||
maxX = Math.max(maxX, p[0]);
|
||||
pointSet.add(List.of(p[0], p[1]));
|
||||
}
|
||||
long s = minX + maxX;
|
||||
for (int[] point : points) {
|
||||
if (!pointSet.contains((s - point[0]) + "." + point[1])) {
|
||||
int s = minX + maxX;
|
||||
for (int[] p : points) {
|
||||
if (!pointSet.contains(List.of(s - p[0], p[1]))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,4 @@ class Solution:
|
|||
max_x = max(max_x, x)
|
||||
point_set.add((x, y))
|
||||
s = min_x + max_x
|
||||
for x, y in points:
|
||||
if (s - x, y) not in point_set:
|
||||
return False
|
||||
return True
|
||||
return all((s - x, y) in point_set for x, y in points)
|
||||
|
|
|
|||
Loading…
Reference in New Issue