{"id":1942,"date":"2016-04-04T15:39:51","date_gmt":"2016-04-04T07:39:51","guid":{"rendered":"http:\/\/boweihe.me\/?p=1942"},"modified":"2016-04-04T15:39:51","modified_gmt":"2016-04-04T07:39:51","slug":"leetcode-335-self-crossing","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1942","title":{"rendered":"LeetCode 335. Self Crossing"},"content":{"rendered":"<h3>\u9898\u76ee\uff1a<\/h3>\n<p>You are given an array <i>x<\/i> of <code>n<\/code> positive numbers. You start at point <code>(0,0)<\/code> and moves <code>x[0]<\/code> metres to the north, then <code>x[1]<\/code> metres to the west, <code>x[2]<\/code> metres to the south,<code>x[3]<\/code> metres to the east and so on. In other words, after each move your direction changes counter-clockwise.<br \/>\nWrite a one-pass algorithm with <code>O(1)<\/code> extra space to determine, if your path crosses itself, or not.<br \/>\n<b>Example 1:<\/b><\/p>\n<pre>Given <i>x<\/i> = <code>[2, 1, 1, 2]<\/code>,\n\u250c\u2500\u2500\u2500\u2510\n\u2502   \u2502\n\u2514\u2500\u2500\u2500\u253c\u2500\u2500&gt;\n    \u2502\nReturn <b>true<\/b> (self crossing)\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre>Given <i>x<\/i> = <code>[1, 2, 3, 4]<\/code>,\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502      \u2502\n\u2502\n\u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500&gt;\nReturn <b>false<\/b> (not self crossing)\n<\/pre>\n<p><b>Example 3:<\/b><\/p>\n<pre class=\"\">Given <i>x<\/i> = <code>[1, 1, 1, 1]<\/code>,\n\u250c\u2500\u2500\u2500\u2510\n\u2502   \u2502\n\u2514\u2500\u2500\u2500\u253c&gt;\nReturn <b>true<\/b> (self crossing)\n<\/pre>\n<h3>\u601d\u8def\uff1a<\/h3>\n<p><a href=\"https:\/\/leetcode.com\/discuss\/88054\/java-oms-with-explanation\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/leetcode.com\/discuss\/88054\/java-oms-with-explanation<\/a><br \/>\n\u672c\u4eba\u611a\u949d\uff0c\u60f3\u4e0d\u5230O(1)\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u65b9\u6cd5\uff0c\u53ea\u80fd\u770bDiscussion\u5566\uff0c\u770b\u5b8c\u4ee5\u540e\u53d1\u73b0\uff0c\u9047\u5230\u95ee\u9898\u80fd\u591f\u5206\u6790\u51fa\u5404\u79cd\u60c5\u51b5\u771f\u662f\u4e0d\u5bb9\u6613\u3002<br \/>\n\u628a\u5927\u795e\u7684\u4ee3\u7801\u603b\u7ed3\u4e00\u4e0b\uff0c\u53ef\u4ee5\u53d8\u6210\u4e0b\u9762\u8fd9\u5f20\u56fe\uff0c\u56fe\u4e2d\u7ed9\u51fa\u4e86\u53ef\u80fd\u53d1\u751fself-crossing\u7684\u6240\u6709\u60c5\u51b5\uff08\u5176\u4ed6\u60c5\u51b5\u90fd\u662f\u8fd9\u4e9b\u60c5\u51b5\u7684&#8221;\u65cb\u8f6c&#8221;\u7248\u672c\uff09\uff1a<br \/>\n<a href=\"http:\/\/boweihe.me\/wp-content\/uploads\/2016\/04\/LeetCode_Self_Crossing.png\" rel=\"attachment wp-att-1943\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1943\" src=\"http:\/\/boweihe.me\/wp-content\/uploads\/2016\/04\/LeetCode_Self_Crossing.png\" alt=\"LeetCode_Self_Crossing\" width=\"1440\" height=\"500\" \/><\/a><br \/>\n\u6240\u4ee5\u5c31\u662f\u904d\u5386\u6240\u6709\u79fb\u52a8\u6b65x[i]\uff0c\u5224\u65ad\u4e4b\u524d\u7684\u60c5\u51b5\u662f\u5426\u5728\u8fd9\u4e09\u79cd\u60c5\u51b5\u4e4b\u5185\uff0c\u5982\u679c\u6709\u7684\u8bdd\uff0c\u90a3\u80af\u5b9a\u5c31\u6709crossing\u4e86\u3002\u5176\u4e2d\u60c5\u51b52\u7684\u610f\u601d\u662f\uff0c\u5f53\u524d\u7684\u6b65\u8fdbx[i]\u8986\u76d6\u5230\u4e86x[i-4]\u4e86\uff0c\u5176\u4ed6\u60c5\u51b5\u770b\u56fe\u5e94\u8be5\u90fd\u597d\u7406\u89e3\u7684\u3002<\/p>\n<h3>\u4ee3\u7801\uff1a<\/h3>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    bool isSelfCrossing(vector&lt;int&gt;&amp; x) {\n        int numSz = x.size();\n        if(numSz &lt;= 3)\n            return false;\n        for(int i=3; i&lt;numSz; i++){\n            if(x[i]&gt;=x[i-2] &amp;&amp; x[i-1]&lt;=x[i-3]) \/\/Cond 1\n                return true;\n            else if(i&gt;=4 &amp;&amp; x[i-1]==x[i-3] &amp;&amp; x[i]+x[i-4]&gt;=x[i-2])\/\/Cond 2\n                return true;\n            else if(i&gt;=5 &amp;&amp; x[i-1]&lt;=x[i-3] &amp;&amp; x[i-4]&lt;=x[i-2] &amp;&amp; x[i]+x[i-4]&gt;=x[i-2] &amp;&amp; x[i-1]+x[i-5]&gt;=x[i-3]) \/\/Cond 3\n                return true;\n        }\n        return false;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\uff1a You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to the north, then x[1] metres to the west, x[2] metres to the south,x[3] metres to the east and so on. In other words, after each move your direction changes counter-clockwise. Write a one-pass algorithm [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66],"class_list":["post-1942","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1942","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1942"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1942\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}