{"id":5684,"date":"2019-10-01T00:40:13","date_gmt":"2019-10-01T07:40:13","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5684"},"modified":"2019-10-01T00:40:56","modified_gmt":"2019-10-01T07:40:56","slug":"leetcode-71-simplify-path","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/stack\/leetcode-71-simplify-path\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 71. Simplify Path"},"content":{"rendered":"\n<p>Given an&nbsp;<strong>absolute path<\/strong>&nbsp;for a file (Unix-style), simplify it. Or in other words, convert it to the&nbsp;<strong>canonical path<\/strong>.<\/p>\n\n\n\n<p>In a UNIX-style file system, a period&nbsp;<code>.<\/code>&nbsp;refers to the current directory. Furthermore, a double period&nbsp;<code>..<\/code>&nbsp;moves the directory up a level. For more information, see:&nbsp;<a href=\"https:\/\/www.linuxnix.com\/abslute-path-vs-relative-path-in-linuxunix\/\" target=\"_blank\" rel=\"noreferrer noopener\">Absolute path&nbsp;vs&nbsp;relative&nbsp;path&nbsp;in&nbsp;Linux\/Unix<\/a><\/p>\n\n\n\n<p>Note that the returned canonical path must always begin&nbsp;with a slash&nbsp;<code>\/<\/code>, and there must be only a single slash&nbsp;<code>\/<\/code>&nbsp;between two directory names.&nbsp;The last directory name (if it exists)&nbsp;<strong>must not<\/strong>&nbsp;end with a trailing&nbsp;<code>\/<\/code>. Also, the canonical path must be the&nbsp;<strong>shortest<\/strong>&nbsp;string&nbsp;representing the absolute path.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: \"<\/strong>\/home\/\"\n<strong>Output: \"<\/strong>\/home\"\n<strong>Explanation:<\/strong> Note that there is no trailing slash after the last directory name.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: \"<\/strong>\/..\/\"\n<strong>Output: \"<\/strong>\/\"\n<strong>Explanation:<\/strong> Going one level up from the root directory is a no-op, as the root level is the highest level you can go.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: \"<\/strong>\/home\/\/foo\/\"\n<strong>Output: \"<\/strong>\/home\/foo\"\n<strong>Explanation: <\/strong>In the canonical path, multiple consecutive slashes are replaced by a single one.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: \"<\/strong>\/a\/.\/b\/..\/..\/c\/\"\n<strong>Output: \"<\/strong>\/c\"\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: \"<\/strong>\/a\/..\/..\/b\/..\/c\/\/.\/\/\"\n<strong>Output: \"<\/strong>\/c\"\n<\/pre>\n\n\n\n<p><strong>Example 6:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: \"<\/strong>\/a\/\/b\/\/\/\/c\/d\/\/.\/.\/\/..\"\n<strong>Output: \"<\/strong>\/a\/b\/c\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Stack<\/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  string simplifyPath(string path) {\n    vector<string> s;\n    int start = 1;\n    for (int i = 1; i <= path.length(); ++i) {\n      if (i == path.length() || path[i] == '\/') {\n        string p = path.substr(start, i - start);\n        if (p == \"\/\") continue;\n        if (p == \"..\") {\n          if (!s.empty()) s.pop_back();\n        } else if (p.length() > 0 && p != \".\") {\n          s.push_back(std::move(p));\n        }\n        start = i + 1;\n      }\n    }\n\n    string ans;\n    for (int i = 0; i < s.size(); ++i)\n      ans += \"\/\" + s[i];\n    return ans.empty() ? \"\/\" : ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an&nbsp;absolute path&nbsp;for a file (Unix-style), simplify it. Or in other words, convert it to the&nbsp;canonical path. In a UNIX-style file system, a period&nbsp;.&nbsp;refers to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[407],"tags":[177,180,4],"class_list":["post-5684","post","type-post","status-publish","format-standard","hentry","category-stack","tag-medium","tag-stack","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5684","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=5684"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5684\/revisions"}],"predecessor-version":[{"id":5687,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5684\/revisions\/5687"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}