Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 056bda7

Browse files
committed
feat: add solutions to lc problem: No.0593
No.0593.Valid Square
1 parent 66adda9 commit 056bda7

File tree

6 files changed

+285
-2
lines changed

6 files changed

+285
-2
lines changed

‎solution/0500-0599/0593.Valid Square/README.md‎

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,123 @@
4848

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

51+
**方法一:数学**
52+
53+
若任选三个点,都能构成等腰直角三角形,说明是有效的正方形。
54+
55+
时间复杂度 $O(1)$。
56+
5157
<!-- tabs:start -->
5258

5359
### **Python3**
5460

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

5763
```python
58-
64+
class Solution:
65+
def validSquare(
66+
self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]
67+
) -> bool:
68+
def check(a, b, c):
69+
(x1, y1), (x2, y2), (x3, y3) = a, b, c
70+
d1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)
71+
d2 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)
72+
d3 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3)
73+
return any(
74+
[
75+
d1 == d2 and d1 + d2 == d3 and d1,
76+
d2 == d3 and d2 + d3 == d1 and d2,
77+
d1 == d3 and d1 + d3 == d2 and d1,
78+
]
79+
)
80+
81+
return (
82+
check(p1, p2, p3)
83+
and check(p2, p3, p4)
84+
and check(p1, p3, p4)
85+
and check(p1, p2, p4)
86+
)
5987
```
6088

6189
### **Java**
6290

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

6593
```java
94+
class Solution {
95+
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
96+
return check(p1, p2, p3) && check(p1, p3, p4) && check(p1, p2, p4) && check(p2, p3, p4);
97+
}
98+
99+
private boolean check(int[] a, int[] b, int[] c) {
100+
int x1 = a[0], y1 = a[1];
101+
int x2 = b[0], y2 = b[1];
102+
int x3 = c[0], y3 = c[1];
103+
int d1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
104+
int d2 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
105+
int d3 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3);
106+
if (d1 == d2 && d1 + d2 == d3 && d1 > 0) {
107+
return true;
108+
}
109+
if (d1 == d3 && d1 + d3 == d2 && d1 > 0) {
110+
return true;
111+
}
112+
if (d2 == d3 && d2 + d3 == d1 && d2 > 0) {
113+
return true;
114+
}
115+
return false;
116+
}
117+
}
118+
```
119+
120+
### **C++**
121+
122+
```cpp
123+
class Solution {
124+
public:
125+
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
126+
return check(p1, p2, p3) && check(p1, p3, p4) && check(p1, p2, p4) && check(p2, p3, p4);
127+
}
128+
129+
bool check(vector<int>& a, vector<int>& b, vector<int>& c) {
130+
int x1 = a[0], y1 = a[1];
131+
int x2 = b[0], y2 = b[1];
132+
int x3 = c[0], y3 = c[1];
133+
int d1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
134+
int d2 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
135+
int d3 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3);
136+
if (d1 == d2 && d1 + d2 == d3 && d1 > 0) return true;
137+
if (d1 == d3 && d1 + d3 == d2 && d1 > 0) return true;
138+
if (d2 == d3 && d2 + d3 == d1 && d2 > 0) return true;
139+
return false;
140+
}
141+
};
142+
```
66143

144+
### **Go**
145+
146+
```go
147+
func validSquare(p1 []int, p2 []int, p3 []int, p4 []int) bool {
148+
check := func(a, b, c []int) bool {
149+
x1, y1 := a[0], a[1]
150+
x2, y2 := b[0], b[1]
151+
x3, y3 := c[0], c[1]
152+
d1 := (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)
153+
d2 := (x1-x3)*(x1-x3) + (y1-y3)*(y1-y3)
154+
d3 := (x2-x3)*(x2-x3) + (y2-y3)*(y2-y3)
155+
if d1 == d2 && d1+d2 == d3 && d1 > 0 {
156+
return true
157+
}
158+
if d1 == d3 && d1+d3 == d2 && d1 > 0 {
159+
return true
160+
}
161+
if d2 == d3 && d2+d3 == d1 && d2 > 0 {
162+
return true
163+
}
164+
return false
165+
}
166+
return check(p1, p2, p3) && check(p1, p3, p4) && check(p1, p2, p4) && check(p2, p3, p4)
167+
}
67168
```
68169

69170
### **...**

‎solution/0500-0599/0593.Valid Square/README_EN.md‎

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,108 @@
4747
### **Python3**
4848

4949
```python
50-
50+
class Solution:
51+
def validSquare(
52+
self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]
53+
) -> bool:
54+
def check(a, b, c):
55+
(x1, y1), (x2, y2), (x3, y3) = a, b, c
56+
d1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)
57+
d2 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)
58+
d3 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3)
59+
return any(
60+
[
61+
d1 == d2 and d1 + d2 == d3 and d1,
62+
d2 == d3 and d2 + d3 == d1 and d2,
63+
d1 == d3 and d1 + d3 == d2 and d1,
64+
]
65+
)
66+
67+
return (
68+
check(p1, p2, p3)
69+
and check(p2, p3, p4)
70+
and check(p1, p3, p4)
71+
and check(p1, p2, p4)
72+
)
5173
```
5274

5375
### **Java**
5476

5577
```java
78+
class Solution {
79+
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
80+
return check(p1, p2, p3) && check(p1, p3, p4) && check(p1, p2, p4) && check(p2, p3, p4);
81+
}
82+
83+
private boolean check(int[] a, int[] b, int[] c) {
84+
int x1 = a[0], y1 = a[1];
85+
int x2 = b[0], y2 = b[1];
86+
int x3 = c[0], y3 = c[1];
87+
int d1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
88+
int d2 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
89+
int d3 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3);
90+
if (d1 == d2 && d1 + d2 == d3 && d1 > 0) {
91+
return true;
92+
}
93+
if (d1 == d3 && d1 + d3 == d2 && d1 > 0) {
94+
return true;
95+
}
96+
if (d2 == d3 && d2 + d3 == d1 && d2 > 0) {
97+
return true;
98+
}
99+
return false;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
110+
return check(p1, p2, p3) && check(p1, p3, p4) && check(p1, p2, p4) && check(p2, p3, p4);
111+
}
112+
113+
bool check(vector<int>& a, vector<int>& b, vector<int>& c) {
114+
int x1 = a[0], y1 = a[1];
115+
int x2 = b[0], y2 = b[1];
116+
int x3 = c[0], y3 = c[1];
117+
int d1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
118+
int d2 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
119+
int d3 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3);
120+
if (d1 == d2 && d1 + d2 == d3 && d1 > 0) return true;
121+
if (d1 == d3 && d1 + d3 == d2 && d1 > 0) return true;
122+
if (d2 == d3 && d2 + d3 == d1 && d2 > 0) return true;
123+
return false;
124+
}
125+
};
126+
```
56127

128+
### **Go**
129+
130+
```go
131+
func validSquare(p1 []int, p2 []int, p3 []int, p4 []int) bool {
132+
check := func(a, b, c []int) bool {
133+
x1, y1 := a[0], a[1]
134+
x2, y2 := b[0], b[1]
135+
x3, y3 := c[0], c[1]
136+
d1 := (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)
137+
d2 := (x1-x3)*(x1-x3) + (y1-y3)*(y1-y3)
138+
d3 := (x2-x3)*(x2-x3) + (y2-y3)*(y2-y3)
139+
if d1 == d2 && d1+d2 == d3 && d1 > 0 {
140+
return true
141+
}
142+
if d1 == d3 && d1+d3 == d2 && d1 > 0 {
143+
return true
144+
}
145+
if d2 == d3 && d2+d3 == d1 && d2 > 0 {
146+
return true
147+
}
148+
return false
149+
}
150+
return check(p1, p2, p3) && check(p1, p3, p4) && check(p1, p2, p4) && check(p2, p3, p4)
151+
}
57152
```
58153

59154
### **...**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
4+
return check(p1, p2, p3) && check(p1, p3, p4) && check(p1, p2, p4) && check(p2, p3, p4);
5+
}
6+
7+
bool check(vector<int>& a, vector<int>& b, vector<int>& c) {
8+
int x1 = a[0], y1 = a[1];
9+
int x2 = b[0], y2 = b[1];
10+
int x3 = c[0], y3 = c[1];
11+
int d1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
12+
int d2 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
13+
int d3 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3);
14+
if (d1 == d2 && d1 + d2 == d3 && d1 > 0) return true;
15+
if (d1 == d3 && d1 + d3 == d2 && d1 > 0) return true;
16+
if (d2 == d3 && d2 + d3 == d1 && d2 > 0) return true;
17+
return false;
18+
}
19+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func validSquare(p1 []int, p2 []int, p3 []int, p4 []int) bool {
2+
check := func(a, b, c []int) bool {
3+
x1, y1 := a[0], a[1]
4+
x2, y2 := b[0], b[1]
5+
x3, y3 := c[0], c[1]
6+
d1 := (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)
7+
d2 := (x1-x3)*(x1-x3) + (y1-y3)*(y1-y3)
8+
d3 := (x2-x3)*(x2-x3) + (y2-y3)*(y2-y3)
9+
if d1 == d2 && d1+d2 == d3 && d1 > 0 {
10+
return true
11+
}
12+
if d1 == d3 && d1+d3 == d2 && d1 > 0 {
13+
return true
14+
}
15+
if d2 == d3 && d2+d3 == d1 && d2 > 0 {
16+
return true
17+
}
18+
return false
19+
}
20+
return check(p1, p2, p3) && check(p1, p3, p4) && check(p1, p2, p4) && check(p2, p3, p4)
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
3+
return check(p1, p2, p3) && check(p1, p3, p4) && check(p1, p2, p4) && check(p2, p3, p4);
4+
}
5+
6+
private boolean check(int[] a, int[] b, int[] c) {
7+
int x1 = a[0], y1 = a[1];
8+
int x2 = b[0], y2 = b[1];
9+
int x3 = c[0], y3 = c[1];
10+
int d1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
11+
int d2 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
12+
int d3 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3);
13+
if (d1 == d2 && d1 + d2 == d3 && d1 > 0) {
14+
return true;
15+
}
16+
if (d1 == d3 && d1 + d3 == d2 && d1 > 0) {
17+
return true;
18+
}
19+
if (d2 == d3 && d2 + d3 == d1 && d2 > 0) {
20+
return true;
21+
}
22+
return false;
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def validSquare(
3+
self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]
4+
) -> bool:
5+
def check(a, b, c):
6+
(x1, y1), (x2, y2), (x3, y3) = a, b, c
7+
d1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)
8+
d2 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)
9+
d3 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3)
10+
return any(
11+
[
12+
d1 == d2 and d1 + d2 == d3 and d1,
13+
d2 == d3 and d2 + d3 == d1 and d2,
14+
d1 == d3 and d1 + d3 == d2 and d1,
15+
]
16+
)
17+
18+
return (
19+
check(p1, p2, p3)
20+
and check(p2, p3, p4)
21+
and check(p1, p3, p4)
22+
and check(p1, p2, p4)
23+
)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /