{"id":744,"date":"2020-09-26T11:17:46","date_gmt":"2020-09-26T03:17:46","guid":{"rendered":"http:\/\/39.96.58.60\/?p=744"},"modified":"2022-10-18T16:37:28","modified_gmt":"2022-10-18T08:37:28","slug":"leetcode-127-%e5%8d%95%e8%af%8d%e6%8e%a5%e9%be%99","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=744","title":{"rendered":"Leetcode 127. \u5355\u8bcd\u63a5\u9f99"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e24\u4e2a\u5355\u8bcd\uff08<em>beginWord&nbsp;<\/em>\u548c <em>endWord<\/em>\uff09\u548c\u4e00\u4e2a\u5b57\u5178\uff0c\u627e\u5230\u4ece&nbsp;<em>beginWord<\/em> \u5230&nbsp;<em>endWord<\/em> \u7684\u6700\u77ed\u8f6c\u6362\u5e8f\u5217\u7684\u957f\u5ea6\u3002\u8f6c\u6362\u9700\u9075\u5faa\u5982\u4e0b\u89c4\u5219\uff1a<\/p>\n<ol>\n<li>\u6bcf\u6b21\u8f6c\u6362\u53ea\u80fd\u6539\u53d8\u4e00\u4e2a\u5b57\u6bcd\u3002<\/li>\n<li>\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u7684\u4e2d\u95f4\u5355\u8bcd\u5fc5\u987b\u662f\u5b57\u5178\u4e2d\u7684\u5355\u8bcd\u3002<\/li>\n<\/ol>\n<p><strong>\u8bf4\u660e:<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u4e0d\u5b58\u5728\u8fd9\u6837\u7684\u8f6c\u6362\u5e8f\u5217\uff0c\u8fd4\u56de 0\u3002<\/li>\n<li>\u6240\u6709\u5355\u8bcd\u5177\u6709\u76f8\u540c\u7684\u957f\u5ea6\u3002<\/li>\n<li>\u6240\u6709\u5355\u8bcd\u53ea\u7531\u5c0f\u5199\u5b57\u6bcd\u7ec4\u6210\u3002<\/li>\n<li>\u5b57\u5178\u4e2d\u4e0d\u5b58\u5728\u91cd\u590d\u7684\u5355\u8bcd\u3002<\/li>\n<li>\u4f60\u53ef\u4ee5\u5047\u8bbe <em>beginWord<\/em> \u548c <em>endWord <\/em>\u662f\u975e\u7a7a\u7684\uff0c\u4e14\u4e8c\u8005\u4e0d\u76f8\u540c\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b&nbsp;1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong>\n\nbeginWord = \"hit\",\n\nendWord = \"cog\",\n\nwordList = [\"hot\",\"dot\",\"dog\",\"lot\",\"log\",\"cog\"]\n\n\n\n<strong>\u8f93\u51fa: <\/strong>5\n\n\n\n<strong>\u89e3\u91ca: <\/strong>\u4e00\u4e2a\u6700\u77ed\u8f6c\u6362\u5e8f\u5217\u662f \"hit\" -&gt; \"hot\" -&gt; \"dot\" -&gt; \"dog\" -&gt; \"cog\",\n\n     \u8fd4\u56de\u5b83\u7684\u957f\u5ea6 5\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong>\n\nbeginWord = \"hit\"\n\nendWord = \"cog\"\n\nwordList = [\"hot\",\"dot\",\"dog\",\"lot\",\"log\"]\n\n\n\n<strong>\u8f93\u51fa:<\/strong>&nbsp;0\n\n\n\n<strong>\u89e3\u91ca:<\/strong>&nbsp;<em>endWord<\/em> \"cog\" \u4e0d\u5728\u5b57\u5178\u4e2d\uff0c\u6240\u4ee5\u65e0\u6cd5\u8fdb\u884c\u8f6c\u6362\u3002<\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\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\uff1a100 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8698.78% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.2 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8681.88% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def:\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -&gt; int:\n        if beginWord == endWord:\n            return 1\n        if endWord not in wordList or len(beginWord) != len(endWord):\n            return 0\n\n        wordSet, record = set(wordList), set()\n        head, tail = {beginWord}, {endWord} # \u5934\uff0c\u5c3e\u96c6\u5408\n        result = 1\n        while head:\n            wordSet -= record   # \u603b\u7684wordlist \u51cf\u53bb record\u672a\u5339\u914d\u6210\u7684\u3002\n            record = set()      # \u7528\u4e8e\u8bb0\u5f55\u672a\u5339\u914d\u7684\n            if len(head) &gt; len(tail):\n                head, tail = tail, head # \u4e3a\u4e86\u51cf\u5c11\u5faa\u73af\u6b21\u6570\uff0c\u6bcf\u6b21\u4ece\u6700\u77ed\u7684\u96c6\u5408\u5f00\u59cb\u3002 \u4e5f\u5c31\u662f\u6bcf\u6b21\u4ece\u5f53\u524dwordset\u4e2d \u5339\u914d\u5934\u5c3e\u96c6\u5408\u4e2d\u6700\u77ed\u7684\u90a3\u4e2a\n\n            for word in head:\n                for i in range(len(beginWord)):\n                    for c in 'abcdefghijklmnopqrstuvwxyz':\n                        now = word[:i]+c+word[i+1:] # \u8f6c\u6362\u4e00\u4e2a\u5b57\u7b26\u540e\u7684\u5355\u8bcd\n\n                        if now in tail: # \u5982\u679c\u5f53\u524d\u5355\u8bcd\u5728\u53e6\u4e00\u4e2a\u96c6\u5408\u4e2d\uff0c\u5219\u53ef\u4ee5\u8f6c\u6362\u6210\u529f\uff0c\u8fd4\u56de\u5f53\u524d\u8f6c\u6362\u6b21\u6570\u5373\u53ef\n                            return result+1\n                        if now in wordSet:  # \u5982\u679c\u5f53\u524d\u5355\u8bcd\u5728wordset\u4e2d\uff0c\u5373\u5f53\u524d\u5355\u8bcd\u5b58\u5728\u4f46\u4e0d\u80fd\u5339\u914d\uff0c\u6dfb\u52a0\u5230\u672a\u5339\u914d\u6210\u529f\u96c6\u5408\u4e2d\n                            record.add(now)\n            head = record   #\n            result += 1\n        return 0<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e24\u4e2a\u5355\u8bcd\uff08beginWord&nbsp;\u548c endWord\uff09\u548c\u4e00\u4e2a\u5b57\u5178\uff0c\u627e\u5230\u4ece&nbsp;beginWor&#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\/744"}],"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=744"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/744\/revisions"}],"predecessor-version":[{"id":745,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/744\/revisions\/745"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=744"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}