{"id":6993,"date":"2020-06-28T00:41:25","date_gmt":"2020-06-28T07:41:25","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6993"},"modified":"2020-06-28T00:42:35","modified_gmt":"2020-06-28T07:42:35","slug":"leetcode-1496-path-crossing","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1496-path-crossing\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1496. Path Crossing"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>path<\/code>, where&nbsp;<code>path[i] =&nbsp;'N'<\/code>,&nbsp;<code>'S'<\/code>,&nbsp;<code>'E'<\/code>&nbsp;or&nbsp;<code>'W'<\/code>, each representing moving one unit north, south, east, or west, respectively. You start at the origin&nbsp;<code>(0, 0)<\/code>&nbsp;on a 2D plane and walk on the path specified by&nbsp;<code>path<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<code>True<\/code>&nbsp;if the path crosses itself at any point, that is, if at any time you are on a location you&#8217;ve previously visited. Return&nbsp;<code>False<\/code>&nbsp;otherwise.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/06\/10\/screen-shot-2020-06-10-at-123929-pm.png\" alt=\"\" width=\"250\" height=\"224\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> path = \"NES\"\n<strong>Output:<\/strong> false \n<strong>Explanation:<\/strong> Notice that the path doesn't cross any point more than once.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/06\/10\/screen-shot-2020-06-10-at-123843-pm.png\" alt=\"\" width=\"258\" height=\"219\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> path = \"NESWW\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> Notice that the path visits the origin twice.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= path.length &lt;= 10^4<\/code><\/li><li><code>path<\/code>&nbsp;will only consist of characters in&nbsp;<code>{'N', 'S', 'E', 'W}<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation + Hashtable<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool isPathCrossing(string path) {\n    unordered_set<int> s;\n    int x = 0;\n    int y = 0;\n    s.insert(x * 10000 + y);\n    for (char d : path) {\n      if (d == 'N') ++y;\n      else if (d == 'S') --y;\n      else if (d == 'E') ++x;\n      else if (d == 'W') --x;\n      if (!s.insert(x * 10000 + y).second)\n        return true;\n    }\n    return false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;path, where&nbsp;path[i] =&nbsp;&#8216;N&#8217;,&nbsp;&#8216;S&#8217;,&nbsp;&#8216;E&#8217;&nbsp;or&nbsp;&#8216;W&#8217;, each representing moving one unit north, south, east, or west, respectively. You start at the origin&nbsp;(0, 0)&nbsp;on a 2D plane&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[222,82,179],"class_list":["post-6993","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-easy","tag-hashtable","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6993","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=6993"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6993\/revisions"}],"predecessor-version":[{"id":6995,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6993\/revisions\/6995"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}