{"id":942,"date":"2017-11-27T20:34:21","date_gmt":"2017-11-28T04:34:21","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=942"},"modified":"2018-04-19T08:43:14","modified_gmt":"2018-04-19T15:43:14","slug":"leetcode-657-judge-route-circle","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-657-judge-route-circle\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 657. Judge Route Circle"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"LeetCode 657. Judge Route Circle - \u82b1\u82b1\u9171 \u5237\u9898\u627e\u5de5\u4f5c EP2\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/Vp5qRtuyln4?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to\u00a0<b>the original place<\/b>.<\/p>\n<p>The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are\u00a0<code>R<\/code>\u00a0(Right),\u00a0<code>L<\/code>\u00a0(Left),\u00a0<code>U<\/code>\u00a0(Up) and\u00a0<code>D<\/code>\u00a0(down). The output should be true or false representing whether the robot makes a circle.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: \"UD\"\r\nOutput: true\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: \"LL\"\r\nOutput: false<\/pre>\n<p><strong>Idea:<\/strong><\/p>\n<p>Simulation<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 19 ms\r\nclass Solution {\r\npublic:\r\n    bool judgeCircle(string moves) {\r\n        int cx{0};\r\n        int cy{0};\r\n        for (const char move : moves) {\r\n            switch(move) {\r\n                case 'U':\r\n                    cy--;\r\n                    break;\r\n                case 'D':\r\n                    cy++;\r\n                    break;\r\n                case 'R':\r\n                    cx++;\r\n                    break;\r\n                case 'L':\r\n                    cx--;\r\n                    break;\r\n            }\r\n            \r\n        }\r\n        return cx==0 &amp;&amp; cy==0;\r\n    }    \r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[163,48],"tags":[],"class_list":["post-942","post","type-post","status-publish","format-standard","hentry","category-easy","category-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/942","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=942"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/942\/revisions"}],"predecessor-version":[{"id":2733,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/942\/revisions\/2733"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}