{"id":189,"date":"2022-01-06T06:55:16","date_gmt":"2022-01-05T22:55:16","guid":{"rendered":"https:\/\/www.wennroy.com\/?p=189"},"modified":"2022-01-06T06:56:10","modified_gmt":"2022-01-05T22:56:10","slug":"71-simplify-path","status":"publish","type":"post","link":"https:\/\/wennroy.com\/index.php\/2022\/01\/06\/71-simplify-path\/","title":{"rendered":"71. \u7b80\u5316\u8def\u5f84"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32 path \uff0c\u8868\u793a\u6307\u5411\u67d0\u4e00\u6587\u4ef6\u6216\u76ee\u5f55\u7684&nbsp;Unix \u98ce\u683c \u7edd\u5bf9\u8def\u5f84 \uff08\u4ee5 &#8216;\/&#8217; \u5f00\u5934\uff09\uff0c\u8bf7\u4f60\u5c06\u5176\u8f6c\u5316\u4e3a\u66f4\u52a0\u7b80\u6d01\u7684\u89c4\u8303\u8def\u5f84\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Unix \u98ce\u683c\u7684\u6587\u4ef6\u7cfb\u7edf\u4e2d\uff0c\u4e00\u4e2a\u70b9\uff08.\uff09\u8868\u793a\u5f53\u524d\u76ee\u5f55\u672c\u8eab\uff1b\u6b64\u5916\uff0c\u4e24\u4e2a\u70b9 \uff08..\uff09&nbsp;\u8868\u793a\u5c06\u76ee\u5f55\u5207\u6362\u5230\u4e0a\u4e00\u7ea7\uff08\u6307\u5411\u7236\u76ee\u5f55\uff09\uff1b\u4e24\u8005\u90fd\u53ef\u4ee5\u662f\u590d\u6742\u76f8\u5bf9\u8def\u5f84\u7684\u7ec4\u6210\u90e8\u5206\u3002\u4efb\u610f\u591a\u4e2a\u8fde\u7eed\u7684\u659c\u6760\uff08\u5373\uff0c&#8217;\/\/&#8217;\uff09\u90fd\u88ab\u89c6\u4e3a\u5355\u4e2a\u659c\u6760 &#8216;\/&#8217; \u3002 \u5bf9\u4e8e\u6b64\u95ee\u9898\uff0c\u4efb\u4f55\u5176\u4ed6\u683c\u5f0f\u7684\u70b9\uff08\u4f8b\u5982\uff0c&#8217;\u2026&#8217;\uff09\u5747\u88ab\u89c6\u4e3a\u6587\u4ef6\/\u76ee\u5f55\u540d\u79f0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf7\u6ce8\u610f\uff0c\u8fd4\u56de\u7684 \u89c4\u8303\u8def\u5f84 \u5fc5\u987b\u9075\u5faa\u4e0b\u8ff0\u683c\u5f0f\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u59cb\u7ec8\u4ee5\u659c\u6760 &#8216;\/&#8217; \u5f00\u5934\u3002<br>\u4e24\u4e2a\u76ee\u5f55\u540d\u4e4b\u95f4\u5fc5\u987b\u53ea\u6709\u4e00\u4e2a\u659c\u6760 &#8216;\/&#8217; \u3002<br>\u6700\u540e\u4e00\u4e2a\u76ee\u5f55\u540d\uff08\u5982\u679c\u5b58\u5728\uff09\u4e0d\u80fd \u4ee5 &#8216;\/&#8217; \u7ed3\u5c3e\u3002<br>\u6b64\u5916\uff0c\u8def\u5f84\u4ec5\u5305\u542b\u4ece\u6839\u76ee\u5f55\u5230\u76ee\u6807\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u8def\u5f84\u4e0a\u7684\u76ee\u5f55\uff08\u5373\uff0c\u4e0d\u542b &#8216;.&#8217; \u6216 &#8216;..&#8217;\uff09\u3002<br>\u8fd4\u56de\u7b80\u5316\u540e\u5f97\u5230\u7684 \u89c4\u8303\u8def\u5f84 \u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b80\u5355\u7684\u6808\u6a21\u62df\uff0c\u7136\u800c\u5728\u9884\u5904\u7406\u7684\u8fc7\u7a0b\u6709\u5f88\u591a\u7ec6\u8282\u9700\u8981\u6ce8\u610f\uff0c\u91cd\u590d\u5f00\u5934&#8221;\/\/\/&#8221;\u7b49\u95ee\u9898\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Solution:\n    def simplifyPath(self, path: str) -> str:\n        # \u91cd\u7ec4\u653e\u5165path_list\uff0c\u4e22\u5f03\"\/\/\"\n        path_list = []\n        n = len(path)\n        left = 0\n        right = 1\n        while True:\n            if left == n - 1:\n                return \"\/\"\n            if path[left] == '\/':\n                left += 1\n                right += 1\n                continue\n            left -= 1\n            right -= 1\n            break\n        while right &lt; n - 1:\n            if path[right] == '\/' and not path[right + 1] == '\/':\n                path_list.append(path[left+1: right].replace(\"\/\",\"\"))\n                left = right\n            right += 1\n        if path[right] == '\/':\n            path_list.append(path[left+1: right].replace(\"\/\",\"\"))\n        else:\n            path_list.append(path[left+1: right+1].replace(\"\/\",\"\"))\n        print(path_list)\n        res = []\n        # \u5224\u65ad\u7236\u7ea7\u76ee\u5f55\u6216\u5f53\u524d\u76ee\u5f55\n        for index, cur_dir in enumerate(path_list):\n            if cur_dir == '.':\n                continue\n            if cur_dir == '..' and len(res) > 0:\n                res.pop()\n                continue\n            if cur_dir == '..':\n                continue\n            res.append(cur_dir)\n        # \u91cd\u7ec4\u8f93\u51fa\u7b54\u6848\n        if res == []:\n            return '\/'\n        res_str = ''\n        for single_res in res:\n            res_str += '\/' + single_res\n\n        return res_str\n          <\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u529b\u6263\u6807\u7b54\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Solution:\n    def simplifyPath(self, path: str) -> str:\n        names = path.split(\"\/\")\n        stack = list()\n        for name in names:\n            if name == \"..\":\n                if stack:\n                    stack.pop()\n            elif name and name != \".\":\n                stack.append(name)\n        return \"\/\" + \"\/\".join(stack)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5fd8\u8bb0python\u81ea\u5e26split\u51fd\u6570\uff0c\u624b\u52a8\u5206\u7ec4\u51fa\u73b0\u5f88\u591a\u95ee\u9898\u3002\u5b57\u7b26\u4e32\u7684.join()\u53ef\u4ee5\u8fd0\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6765\u6e90\uff1a\u529b\u6263\uff08LeetCode\uff09<br>\u94fe\u63a5\uff1ahttps:\/\/leetcode-cn.com\/problems\/simplify-path<br>\u8457\u4f5c\u6743\u5f52\u9886\u6263\u7f51\u7edc\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u5b98\u65b9\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32 path \uff0c\u8868\u793a\u6307\u5411\u67d0\u4e00\u6587\u4ef6\u6216\u76ee\u5f55 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[6,11,12],"tags":[14,13,16],"class_list":["post-189","post","type-post","status-publish","format-standard","hentry","category-leetcode","category-strings","category-stack-simulation","tag-14","tag-13","tag-16"],"_links":{"self":[{"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/posts\/189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/comments?post=189"}],"version-history":[{"count":1,"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/posts\/189\/revisions"}],"predecessor-version":[{"id":190,"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/posts\/189\/revisions\/190"}],"wp:attachment":[{"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/media?parent=189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/categories?post=189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/tags?post=189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}