leetcode 593. Valid Square

论坛 期权论坛     
选择匿名的用户   2021-5-23 02:15   34   0
<p></p>
<p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:&#34;Helvetica Neue&#34;,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:&#34;Helvetica Neue&#34;,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:&#34;Helvetica Neue&#34;,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,&#34;Courier New&#34;,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 &#61; [0,0], p2 &#61; [1,1], p3 &#61; [1,0], p4 &#61; [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:&#34;Helvetica Neue&#34;,Helvetica,Arial,sans-serif; font-size:14px"> </p>
<p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:&#34;Helvetica Neue&#34;,Helvetica,Arial,sans-serif; font-size:14px"> Note:</p>
<ol style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:&#34;Helvetica Neue&#34;,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&#61;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]&#61;&#61;0){
   return false;
  }
  //保证有4个边相同,a^2可能比a大,也可能比a小
  if(allBian[0]&#61;&#61;allBian[3]||allBian[2]&#61;&#61;allBian[5]){
   //a^2比a大
   if(allBian[0]&#61;&#61;allBian[3]){
    //使用勾股定理:a^2&#43;b^2&#61;c^2,而a&#61;b,所以2a^2&#61;c^2
    if(allBian[0]*2&#61;&#61;allBian[5]){
     return true;
    }
   }
   else{//a^2比a小
    if(allBian[5]*2&#61;&#61;allBian[0]){
     return true;
    }
   }
  }
  return false;
}

//返回距离的平方
int getDistance2(int[] p1,int[] p2){
  int distance2&#61;(p1[0]-p2[0])*(p1[0]-p2[0])&#43;(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 &#61; new Valid_Square_593();
  int[] p1 &#61; new int[] { 0, 0 };
  int[] p2 &#61; new int[] { 0, 0 };
  int[] p3 &#61; new int[] { 0, 0 };
  int[] p4 &#61; new int[] { 0, 0 };
  System.out.println(v.validSquare(p1, p2, p3, p4));
}

}</code></pre>我为什么算距离的平方,而不是直接算距离呢?因为一个根号下去,变成double类型,有可能出现精度问题。如根号2&#61;1.4142135623731...,但是1.4142135623731的平方&#61;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&lt;Integer, Integer&gt; map &#61; new HashMap&lt;&gt;();
        int a12 &#61; getDistanceSquare(p1, p2);
        int a13 &#61; getDistanceSquare(p1, p3);
        int a14 &#61; getDistanceSquare(p1, p4);
        int a23 &#61; getDistanceSquare(p2, p3);
        int a24 &#61; getDistanceSquare(p2, p4);
        int a34 &#61; getDistanceSquare(p3, p4);
        if (a12 &#61;&#61; 0 || a13 &#61;&#61; 0 || a14 &#61;&#61; 0 || a23 &#61;&#61; 0 || a24 &#61;&#61; 0 || a34 &#61;&#61; 0){
                return false;
        }
        map.put(a12, map.getOrDefault(a12, 0) &#43; 1);
        map.put(a13, map.getOrDefault(a13, 0) &#43; 1);
        map.put(a14, map.getOrDefault(a14, 0) &#43; 1);
        map.put(a23, map.getOrDefault(a23, 0) &#43; 1);
        map.put(a24, map.getOrDefault(a24, 0) &#43; 1);
        map.put(a34, map.getOrDefault(a34, 0) &#43; 1);
        return map.size() &#61;&#61; 2 &amp;&amp; (map.get(a12) &#61;&#61; 2 || map.get(a12) &#61;&#61; 4);

    }
   
    private int getDistanceSquare(int[] a, int[] b) {
        return (a[0] - b[0]) * (a[0] - b[0]) &#43; (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:&#34;Helvetica Neue&#34;,Helvetica,Arial,sans-serif; line-height:1.7; color:rgb(51,51,51); ma
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP