Skip to content

Commit 9370fa2

Browse files
authored
Merge pull request #1259 from 0xff-dev/593
Add solution and test-cases for problem 593
2 parents 524b8e7 + d823d1d commit 9370fa2

File tree

3 files changed

+87
-26
lines changed

3 files changed

+87
-26
lines changed

leetcode/501-600/0593.Valid-Square/README.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# [593.Valid Square][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given the coordinates of four points in 2D space `p1`, `p2`, `p3` and `p4`, return true if the four points construct a square.
5+
6+
The coordinate of a point pi is represented as `[xi, yi]`. The input is **not** given in any order.
7+
8+
A **valid square** has four equal sides with positive length and four equal angles (90-degree angles).
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
14+
Output: true
1315
```
1416

15-
## 题意
16-
> ...
17-
18-
## 题解
17+
**Example 2:**
1918

20-
### 思路1
21-
> ...
22-
Valid Square
23-
```go
2419
```
20+
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12]
21+
Output: false
22+
```
23+
24+
**Example 3:**
2525

26+
```
27+
Input: p1 = [1,0], p2 = [-1,0], p3 = [0,1], p4 = [0,-1]
28+
Output: true
29+
```
2630

2731
## 结语
2832

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,62 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func isVertical(x1, y1, x2, y2 []int) bool {
4+
// 0
5+
a := y1[0] - x1[0]
6+
// -2
7+
b := y1[1] - x1[1]
8+
// -2
9+
c := y2[0] - x2[0]
10+
// 0
11+
d := y2[1] - x2[1]
12+
return a*c+b*d == 0
13+
}
14+
15+
func distance(a, b []int) int {
16+
x := a[0] - b[0]
17+
y := a[1] - b[1]
18+
return x*x + y*y
19+
}
20+
func edgesEqual(x1, y1, x2, y2 []int) bool {
21+
x1x2 := distance(x1, x2)
22+
x1y2 := distance(x1, y2)
23+
24+
if x1x2 != x1y2 {
25+
return false
26+
}
27+
y1x2 := distance(y1, x2)
28+
y1y2 := distance(y1, y2)
29+
if y1x2 != y1y2 {
30+
return false
31+
}
32+
if y1x2 != x1y2 {
33+
return false
34+
}
35+
return distance(x1, y1) == distance(x2, y2)
36+
}
37+
38+
func Solution(p1 []int, p2 []int, p3 []int, p4 []int) bool {
39+
ps := [][]int{p1, p2, p3, p4}
40+
for i := range 3 {
41+
for j := i + 1; j < 4; j++ {
42+
if ps[i][0] == ps[j][0] && ps[i][1] == ps[j][1] {
43+
return false
44+
}
45+
}
46+
}
47+
groups := [][][]int{
48+
{p1, p2, p3, p4},
49+
{p1, p3, p2, p4},
50+
{p1, p4, p2, p3},
51+
}
52+
for _, g := range groups {
53+
if !isVertical(g[0], g[1], g[2], g[3]) {
54+
continue
55+
}
56+
if !edgesEqual(g[0], g[1], g[2], g[3]) {
57+
continue
58+
}
59+
return true
60+
}
61+
return false
562
}

leetcode/501-600/0593.Valid-Square/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
p1, p2, p3, p4 []int
14+
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{0, 0}, []int{1, 1}, []int{1, 0}, []int{0, 1}, true},
17+
{"TestCase2", []int{0, 0}, []int{1, 1}, []int{1, 0}, []int{0, 12}, false},
18+
{"TestCase3", []int{1, 0}, []int{-1, 0}, []int{0, 1}, []int{0, -1}, true},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.p1, c.p2, c.p3, c.p4)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v",
27+
c.expect, got, c.p1, c.p2, c.p3, c.p4)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)