diff --git a/leetcode/501-600/0593.Valid-Square/README.md b/leetcode/501-600/0593.Valid-Square/README.md index 074e9a2f4..be4a6027a 100644 --- a/leetcode/501-600/0593.Valid-Square/README.md +++ b/leetcode/501-600/0593.Valid-Square/README.md @@ -1,28 +1,32 @@ # [593.Valid Square][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +Given the coordinates of four points in 2D space `p1`, `p2`, `p3` and `p4`, return true if the four points construct a square. + +The coordinate of a point pi is represented as `[xi, yi]`. The input is **not** given in any order. + +A **valid square** has four equal sides with positive length and four equal angles (90-degree angles). **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] +Output: true ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Valid Square -```go ``` +Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12] +Output: false +``` + +**Example 3:** +``` +Input: p1 = [1,0], p2 = [-1,0], p3 = [0,1], p4 = [0,-1] +Output: true +``` ## 结语 diff --git a/leetcode/501-600/0593.Valid-Square/Solution.go b/leetcode/501-600/0593.Valid-Square/Solution.go index d115ccf5e..c94bdcf2d 100644 --- a/leetcode/501-600/0593.Valid-Square/Solution.go +++ b/leetcode/501-600/0593.Valid-Square/Solution.go @@ -1,5 +1,62 @@ package Solution -func Solution(x bool) bool { - return x +func isVertical(x1, y1, x2, y2 []int) bool { + // 0 + a := y1[0] - x1[0] + // -2 + b := y1[1] - x1[1] + // -2 + c := y2[0] - x2[0] + // 0 + d := y2[1] - x2[1] + return a*c+b*d == 0 +} + +func distance(a, b []int) int { + x := a[0] - b[0] + y := a[1] - b[1] + return x*x + y*y +} +func edgesEqual(x1, y1, x2, y2 []int) bool { + x1x2 := distance(x1, x2) + x1y2 := distance(x1, y2) + + if x1x2 != x1y2 { + return false + } + y1x2 := distance(y1, x2) + y1y2 := distance(y1, y2) + if y1x2 != y1y2 { + return false + } + if y1x2 != x1y2 { + return false + } + return distance(x1, y1) == distance(x2, y2) +} + +func Solution(p1 []int, p2 []int, p3 []int, p4 []int) bool { + ps := [][]int{p1, p2, p3, p4} + for i := range 3 { + for j := i + 1; j < 4; j++ { + if ps[i][0] == ps[j][0] && ps[i][1] == ps[j][1] { + return false + } + } + } + groups := [][][]int{ + {p1, p2, p3, p4}, + {p1, p3, p2, p4}, + {p1, p4, p2, p3}, + } + for _, g := range groups { + if !isVertical(g[0], g[1], g[2], g[3]) { + continue + } + if !edgesEqual(g[0], g[1], g[2], g[3]) { + continue + } + return true + } + return false } diff --git a/leetcode/501-600/0593.Valid-Square/Solution_test.go b/leetcode/501-600/0593.Valid-Square/Solution_test.go index 14ff50eb4..32d4b9601 100644 --- a/leetcode/501-600/0593.Valid-Square/Solution_test.go +++ b/leetcode/501-600/0593.Valid-Square/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + p1, p2, p3, p4 []int + expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{0, 0}, []int{1, 1}, []int{1, 0}, []int{0, 1}, true}, + {"TestCase2", []int{0, 0}, []int{1, 1}, []int{1, 0}, []int{0, 12}, false}, + {"TestCase3", []int{1, 0}, []int{-1, 0}, []int{0, 1}, []int{0, -1}, true}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.p1, c.p2, c.p3, c.p4) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v", + c.expect, got, c.p1, c.p2, c.p3, c.p4) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }