{"id":615,"date":"2020-09-26T11:03:38","date_gmt":"2020-09-26T03:03:38","guid":{"rendered":"http:\/\/39.96.58.60\/?p=615"},"modified":"2022-10-18T16:45:04","modified_gmt":"2022-10-18T08:45:04","slug":"leetcode-332-%e9%87%8d%e6%96%b0%e5%ae%89%e6%8e%92%e8%a1%8c%e7%a8%8b","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=615","title":{"rendered":"Leetcode 332. \u91cd\u65b0\u5b89\u6392\u884c\u7a0b"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u673a\u7968\u7684\u5b57\u7b26\u4e32\u4e8c\u7ef4\u6570\u7ec4 <code>[from, to]<\/code>\uff0c\u5b50\u6570\u7ec4\u4e2d\u7684\u4e24\u4e2a\u6210\u5458\u5206\u522b\u8868\u793a\u98de\u673a\u51fa\u53d1\u548c\u964d\u843d\u7684\u673a\u573a\u5730\u70b9\uff0c\u5bf9\u8be5\u884c\u7a0b\u8fdb\u884c\u91cd\u65b0\u89c4\u5212\u6392\u5e8f\u3002\u6240\u6709\u8fd9\u4e9b\u673a\u7968\u90fd\u5c5e\u4e8e\u4e00\u4e2a\u4ece JFK\uff08\u80af\u5c3c\u8fea\u56fd\u9645\u673a\u573a\uff09\u51fa\u53d1\u7684\u5148\u751f\uff0c\u6240\u4ee5\u8be5\u884c\u7a0b\u5fc5\u987b\u4ece JFK \u5f00\u59cb\u3002<\/p>\n<p><strong>\u8bf4\u660e:<\/strong><\/p>\n<ol>\n<li>\u5982\u679c\u5b58\u5728\u591a\u79cd\u6709\u6548\u7684\u884c\u7a0b\uff0c\u4f60\u53ef\u4ee5\u6309\u5b57\u7b26\u81ea\u7136\u6392\u5e8f\u8fd4\u56de\u6700\u5c0f\u7684\u884c\u7a0b\u7ec4\u5408\u3002\u4f8b\u5982\uff0c\u884c\u7a0b [&#8220;JFK&#8221;, &#8220;LGA&#8221;] \u4e0e [&#8220;JFK&#8221;, &#8220;LGB&#8221;] \u76f8\u6bd4\u5c31\u66f4\u5c0f\uff0c\u6392\u5e8f\u66f4\u9760\u524d<\/li>\n<li>\u6240\u6709\u7684\u673a\u573a\u90fd\u7528\u4e09\u4e2a\u5927\u5199\u5b57\u6bcd\u8868\u793a\uff08\u673a\u573a\u4ee3\u7801\uff09\u3002<\/li>\n<li>\u5047\u5b9a\u6240\u6709\u673a\u7968\u81f3\u5c11\u5b58\u5728\u4e00\u79cd\u5408\u7406\u7684\u884c\u7a0b\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><code><strong>\u8f93\u5165: <\/strong><\/code><code>[[\"MUC\", \"LHR\"], [\"JFK\", \"MUC\"], [\"SFO\", \"SJC\"], [\"LHR\", \"SFO\"]]<\/code>\n\n<strong>\u8f93\u51fa: <\/strong><code>[\"JFK\", \"MUC\", \"LHR\", \"SFO\", \"SJC\"]<\/code>\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><code><strong>\u8f93\u5165: <\/strong><\/code><code>[[\"JFK\",\"SFO\"],[\"JFK\",\"ATL\"],[\"SFO\",\"ATL\"],[\"ATL\",\"JFK\"],[\"ATL\",\"SFO\"]]<\/code>\n\n<strong>\u8f93\u51fa: <\/strong><code>[\"JFK\",\"ATL\",\"JFK\",\"SFO\",\"ATL\",\"SFO\"]<\/code>\n\n<strong>\u89e3\u91ca: <\/strong>\u53e6\u4e00\u79cd\u6709\u6548\u7684\u884c\u7a0b\u662f&nbsp;<code>[\"JFK\",\"SFO\",\"ATL\",\"JFK\",\"ATL\",\"SFO\"]<\/code>\u3002\u4f46\u662f\u5b83\u81ea\u7136\u6392\u5e8f\u66f4\u5927\u66f4\u9760\u540e\u3002<\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u3001 \u56fe\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a44 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8695.23% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.2 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8636.92% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56de\u6eaf\n    \u5177\u4f53\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def findItinerary(self, tickets: List[List[str]]) -&gt; List[str]:\n        from collections import defaultdict # \u7531\u4e8e\u5b58\u5728\u6709\u7684\u8282\u70b9\u6ca1\u6709\u6307\u5411\u7684\u60c5\u51b5\uff0c\u9700\u8981\u7ed9\u7ec8\u70b9\u4e00\u4e2a\u9ed8\u8ba4\u503c\n        records = defaultdict(list) # \u5b57\u5178\uff0c\u7528\u4e8e\u5b58\u50a8 \u5f53\u524d\u8282\u70b9\u53ef\u4ee5\u901a\u5411\u7684\u8282\u70b9 {from :[to_1, to_2, to_3, ...]}\n        for ticket in tickets:\n            if ticket[0] not in records:\n                records[ticket[0]] = [ticket[1]]\n            else:\n                records[ticket[0]].append(ticket[1])\n                records[ticket[0]].sort()   # \u4e3a\u4e86\u65b9\u4fbf\u5904\u7406\uff0c\u5bf9\u901a\u5411\u7684\u8282\u70b9\u8fdb\u884c\u4e00\u4e2a\u6392\u5e8f\n\n        result = ['JFK']    # result\u4fdd\u5b58\u6700\u7ec8\u7684\u8def\u5f84\uff0c\u5176\u5b9e\u70b9\u4e3a'jfk'\n        def find(start):    # \u56de\u6eaf\n            if len(result) == len(tickets)+1:   # \u5982\u679c\u8def\u5f84\u90fd\u8d70\u8fc7\u4e86\uff0c\u5219\u8fd4\u56detrue  \u3002result\u5305\u542b\u8def\u5f84\u4e0e\u8d77\u59cb\u70b9.\n                return True\n            tos = records[start]    # \u5f53\u524d\u8282\u70b9 \u53ef\u901a\u5411\u7684\u8282\u70b9\n            for to in tos:\n                to = tos.pop(0)     # \u53d6\u51fa\u4e00\u4e2a\u8282\u70b9\uff0c\u52a0\u5165\u6700\u7ec8\u8def\u5f84result\u4e2d\n                result.append(to)\n                if find(to):        # \u4ee5\u5f53\u524d\u8282\u70b9\u7ee7\u7eed\u8d70\uff0c\u5982\u679c\u5f53\u524d\u8282\u70b9\u7b26\u5408\u6700\u7ec8\u8def\u5f84\u8981\u6c42\uff0creturn\n                    return True\n                result.pop()        # \u5f53\u524d\u8282\u70b9\u7ee7\u7eed\u8d70\uff0c\u4e0d\u7b26\u5408\u6700\u7ec8\u8def\u5f84\u8981\u6c42\uff0c\u56de\u6eaf\uff0c\u5c06\u5f53\u524d\u8282\u70b9\u4ece\u6700\u7ec8\u8def\u5f84\u4e2d\u5220\u9664\n                records[start].append(to)   # \u5c06\u5f53\u524d\u8282\u70b9\u5b58\u56de \u5b57\u5178\u4e2d\n            return False\n\n        find(start='JFK')\n        return result<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u673a\u7968\u7684\u5b57\u7b26\u4e32\u4e8c\u7ef4\u6570\u7ec4 [from, to]\uff0c\u5b50\u6570\u7ec4\u4e2d\u7684\u4e24\u4e2a\u6210\u5458\u5206\u522b\u8868\u793a\u98de\u673a\u51fa\u53d1\u548c\u964d\u843d\u7684\u673a\u573a\u5730\u70b9\uff0c\u5bf9\u8be5\u884c&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/615"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=615"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/615\/revisions"}],"predecessor-version":[{"id":616,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/615\/revisions\/616"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=615"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}