{"id":2397,"date":"2018-03-30T08:56:03","date_gmt":"2018-03-30T15:56:03","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2397"},"modified":"2018-04-01T23:41:31","modified_gmt":"2018-04-02T06:41:31","slug":"leetcode-593-valid-square","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-593-valid-square\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 593. Valid Square"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u56db\u4e2a\u70b9\u5224\u65ad\u80fd\u5426\u6784\u6210\u6b63\u65b9\u5f62\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/valid-square\/description\/\">https:\/\/leetcode.com\/problems\/valid-square\/description\/<\/a><\/p>\n<p>Given the coordinates of four points in 2D space, return whether the four points could construct a square.<\/p>\n<p>The coordinate (x,y) of a point is represented by an integer array with two integers.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"crayon:false \"><b>Input:<\/b> p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]\r\n<b>Output:<\/b> True\r\n<\/pre>\n<p>Note:<\/p>\n<ol>\n<li>All the input integers are in the range [-10000, 10000].<\/li>\n<li>A valid square has four equal sides with positive length and four equal angles (90-degree angles).<\/li>\n<li>Input points have no order.<\/li>\n<\/ol>\n<h1><strong>Solution<\/strong><\/h1>\n<p>Time complexity: O(1)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 8 ms\r\nclass Solution {\r\npublic:\r\n  bool validSquare(vector&lt;int&gt;&amp; p1, vector&lt;int&gt;&amp; p2, vector&lt;int&gt;&amp; p3, vector&lt;int&gt;&amp; p4) {\r\n    set&lt;int&gt; s{distSqr(p1, p2), distSqr(p1, p3), distSqr(p1, p4), distSqr(p2, p3), distSqr(p2, p4), distSqr(p3, p4)};\r\n    return !s.count(0) &amp;&amp; s.size() == 2;\r\n  }\r\nprivate:\r\n  static inline int distSqr(const vector&lt;int&gt;&amp; p1, const vector&lt;int&gt;&amp; p2) {\r\n    return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]);\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u56db\u4e2a\u70b9\u5224\u65ad\u80fd\u5426\u6784\u6210\u6b63\u65b9\u5f62\u3002 https:\/\/leetcode.com\/problems\/valid-square\/description\/ Given the coordinates of four points in 2D space, return whether the four points could construct a square. The coordinate (x,y) of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[127],"tags":[284,285,286],"class_list":["post-2397","post","type-post","status-publish","format-standard","hentry","category-geometry","tag-geometry","tag-square","tag-valid","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2397"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2397\/revisions"}],"predecessor-version":[{"id":2405,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2397\/revisions\/2405"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}