<p></p>
<p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:"Helvetica Neue",Helvetica,Arial,sans-serif; font-size:14px"> Given the coordinates of four points in 2D space, return whether the four points could construct a square.</p>
<p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:"Helvetica Neue",Helvetica,Arial,sans-serif; font-size:14px"> The coordinate (x,y) of a point is represented by an integer array with two integers.</p>
<p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:"Helvetica Neue",Helvetica,Arial,sans-serif; font-size:14px"> <span style="font-weight:700">Example:</span><br style=""> </p>
<pre style="overflow:auto; font-family:Menlo,Monaco,Consolas,"Courier New",monospace; font-size:13px; padding:9.5px; margin-top:0px; margin-bottom:10px; line-height:1.42857; color:rgb(51,51,51); word-break:break-all; word-wrap:break-word; background-color:rgb(245,245,245); border:1px solid rgb(204,204,204)"><span style="font-weight:700">Input:</span> p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
<span style="font-weight:700">Output:</span> True
</pre>
<p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:"Helvetica Neue",Helvetica,Arial,sans-serif; font-size:14px"> </p>
<p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:"Helvetica Neue",Helvetica,Arial,sans-serif; font-size:14px"> Note:</p>
<ol style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:"Helvetica Neue",Helvetica,Arial,sans-serif; font-size:14px"><li style="">All the input integers are in the range [-10000, 10000].</li><li style="">A valid square has four equal sides with positive length and four equal angles (90-degree angles).</li><li style="">Input points have no order.</li></ol> 就是看4个点是否构成的是正方形。
<p></p>
<pre class="blockcode"><code class="language-java">package leetcode;
import java.util.Arrays;
public class Valid_Square_593 {
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
int[] allBian=new int[]{
getDistance2(p1, p2),getDistance2(p1, p3),getDistance2(p1, p4),
getDistance2(p2, p3),getDistance2(p2, p4),getDistance2(p3, p4)
};
Arrays.sort(allBian);
if(allBian[0]==0){
return false;
}
//保证有4个边相同,a^2可能比a大,也可能比a小
if(allBian[0]==allBian[3]||allBian[2]==allBian[5]){
//a^2比a大
if(allBian[0]==allBian[3]){
//使用勾股定理:a^2+b^2=c^2,而a=b,所以2a^2=c^2
if(allBian[0]*2==allBian[5]){
return true;
}
}
else{//a^2比a小
if(allBian[5]*2==allBian[0]){
return true;
}
}
}
return false;
}
//返回距离的平方
int getDistance2(int[] p1,int[] p2){
int distance2=(p1[0]-p2[0])*(p1[0]-p2[0])+(p1[1]-p2[1])*(p1[1]-p2[1]);
return distance2;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Valid_Square_593 v = new Valid_Square_593();
int[] p1 = new int[] { 0, 0 };
int[] p2 = new int[] { 0, 0 };
int[] p3 = new int[] { 0, 0 };
int[] p4 = new int[] { 0, 0 };
System.out.println(v.validSquare(p1, p2, p3, p4));
}
}</code></pre>我为什么算距离的平方,而不是直接算距离呢?因为一个根号下去,变成double类型,有可能出现精度问题。如根号2=1.4142135623731...,但是1.4142135623731的平方=2.000000004,不等于2。
<p></p>
<p>大神说:如果4点连成的是正方形,那么这4个点之间的6条线,一定有4条相等,另外2条也相等,且4条的长度不等于2条的长度。4条是边,2条是斜边。</p>
<p></p>
<pre class="blockcode"><code class="language-java">public class Solution {
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
HashMap<Integer, Integer> map = new HashMap<>();
int a12 = getDistanceSquare(p1, p2);
int a13 = getDistanceSquare(p1, p3);
int a14 = getDistanceSquare(p1, p4);
int a23 = getDistanceSquare(p2, p3);
int a24 = getDistanceSquare(p2, p4);
int a34 = getDistanceSquare(p3, p4);
if (a12 == 0 || a13 == 0 || a14 == 0 || a23 == 0 || a24 == 0 || a34 == 0){
return false;
}
map.put(a12, map.getOrDefault(a12, 0) + 1);
map.put(a13, map.getOrDefault(a13, 0) + 1);
map.put(a14, map.getOrDefault(a14, 0) + 1);
map.put(a23, map.getOrDefault(a23, 0) + 1);
map.put(a24, map.getOrDefault(a24, 0) + 1);
map.put(a34, map.getOrDefault(a34, 0) + 1);
return map.size() == 2 && (map.get(a12) == 2 || map.get(a12) == 4);
}
private int getDistanceSquare(int[] a, int[] b) {
return (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]);
}
}</code></pre>这道题有solutions:
<a href="https://leetcode.com/problems/valid-square/solution/" rel="noopener noreferrer" target="_blank">https://leetcode.com/problems/valid-square/solution/</a>。
<p></p>
<h2 id="solution" style="font-family:"Helvetica Neue",Helvetica,Arial,sans-serif; line-height:1.7; color:rgb(51,51,51); ma |
|