|
48 | 48 |
|
49 | 49 | <!-- 这里可写通用的实现逻辑 -->
|
50 | 50 |
|
| 51 | +**方法一:数学** |
| 52 | + |
| 53 | +若任选三个点,都能构成等腰直角三角形,说明是有效的正方形。 |
| 54 | + |
| 55 | +时间复杂度 $O(1)$。 |
| 56 | + |
51 | 57 | <!-- tabs:start -->
|
52 | 58 |
|
53 | 59 | ### **Python3**
|
54 | 60 |
|
55 | 61 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
56 | 62 |
|
57 | 63 | ```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 | + ) |
59 | 87 | ```
|
60 | 88 |
|
61 | 89 | ### **Java**
|
62 | 90 |
|
63 | 91 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
64 | 92 |
|
65 | 93 | ```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 | +``` |
66 | 143 |
|
| 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 | +} |
67 | 168 | ```
|
68 | 169 |
|
69 | 170 | ### **...**
|
|
0 commit comments