{"id":409,"date":"2020-09-26T10:38:17","date_gmt":"2020-09-26T02:38:17","guid":{"rendered":"http:\/\/39.96.58.60\/?p=409"},"modified":"2022-10-18T16:39:24","modified_gmt":"2022-10-18T08:39:24","slug":"leetcode-72-%e7%bc%96%e8%be%91%e8%b7%9d%e7%a6%bb","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=409","title":{"rendered":"Leetcode 72. \u7f16\u8f91\u8ddd\u79bb"},"content":{"rendered":"<p>\u7ed9\u4f60\u4e24\u4e2a\u5355\u8bcd&nbsp;<em>word1<\/em> \u548c&nbsp;<em>word2<\/em>\uff0c\u8bf7\u4f60\u8ba1\u7b97\u51fa\u5c06&nbsp;<em>word1<\/em>&nbsp;\u8f6c\u6362\u6210&nbsp;<em>word2 <\/em>\u6240\u4f7f\u7528\u7684\u6700\u5c11\u64cd\u4f5c\u6570&nbsp;\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u5355\u8bcd\u8fdb\u884c\u5982\u4e0b\u4e09\u79cd\u64cd\u4f5c\uff1a<\/p>\n<ol>\n<li>\u63d2\u5165\u4e00\u4e2a\u5b57\u7b26<\/li>\n<li>\u5220\u9664\u4e00\u4e2a\u5b57\u7b26<\/li>\n<li>\u66ff\u6362\u4e00\u4e2a\u5b57\u7b26<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b&nbsp;1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>word1 = \"horse\", word2 = \"ros\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>3\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\n\nhorse -&gt; rorse (\u5c06 'h' \u66ff\u6362\u4e3a 'r')\n\nrorse -&gt; rose (\u5220\u9664 'r')\n\nrose -&gt; ros (\u5220\u9664 'e')\n\n<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>word1 = \"intention\", word2 = \"execution\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>5\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\n\nintention -&gt; inention (\u5220\u9664 't')\n\ninention -&gt; enention (\u5c06 'i' \u66ff\u6362\u4e3a 'e')\n\nenention -&gt; exention (\u5c06 'n' \u66ff\u6362\u4e3a 'x')\n\nexention -&gt; exection (\u5c06 'n' \u66ff\u6362\u4e3a 'c')\n\nexection -&gt; execution (\u63d2\u5165 'u')\n\n<\/pre>\n<p>**\u96be\u5ea6**: Hard<\/p>\n<p>**\u6807\u7b7e**: \u5b57\u7b26\u4e32\u3001 \u52a8\u6001\u89c4\u5212\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\uff1a216 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8647.71% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a31.7 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.07% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u52a8\u6001\u89c4\u5212\n\n    \u4f8b\uff1a\n            ''  h   o   r   s   e\n        ''  0   1   2   3   4   5\n        r   1   1   2   2   3   4\n        o   2   2   1   2   3   4\n        s   3   3   2   2   2   3\n\n        \u5f53 world2[i] == world1[j]\u65f6\uff0c dp[i][j] = dp[i-1][j-1]\n        \u5f53 world2[i] != world1[j]\u65f6\uff0c\n            1. dp[i][j] = dp[i-1][j]+1      \u53ef\u901a\u8fc7world2[i-1] \u5220\u9664\u4e00\u4e2a\u5143\u7d20\u5f97\u5230 world1[j]\n            2. dp[i][j] = dp[i-1][j-1]+1    \u53ef\u901a\u8fc7world2[i]\uff0c world1[j] \u66ff\u6362\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u5f97\u5230\n            3. dp[i][j] = dp[i][j-1]+1      \u53ef\u901a\u8fc7world2[i] \u63d2\u5165\u4e00\u4e2a\u5143\u7d20\u5f97\u5230 world1[j-1]\uff0c\n\"\"\"\nclass Solution:\n    def minDistance(self, word1: str, word2: str) -&gt; int:\n        m = len(word1)\n        n = len(word2)\n\n        dp = [[[] for _ in range(m+1)] for _ in range(n+1)]\n        for i in range(n+1):\n            dp[i][0] = i\n        dp[0] = list(range(m+1))\n\n        for i in range(n):\n            for j in range(m):\n                if word2[i] == word1[j]:\n                    dp[i+1][j+1] = dp[i][j]\n                else:\n                    dp[i+1][j+1] = min(dp[i][j+1], dp[i+1][j], dp[i][j]) + 1\n        return dp[-1][-1]<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e24\u4e2a\u5355\u8bcd&nbsp;word1 \u548c&nbsp;word2\uff0c\u8bf7\u4f60\u8ba1\u7b97\u51fa\u5c06&nbsp;word1&nbsp;\u8f6c&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","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\/409"}],"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=409"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/409\/revisions"}],"predecessor-version":[{"id":410,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/409\/revisions\/410"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=409"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}