{"id":1054,"date":"2025-12-25T10:04:00","date_gmt":"2025-12-25T02:04:00","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=1054"},"modified":"2025-12-25T10:04:00","modified_gmt":"2025-12-25T02:04:00","slug":"leetcode-72-%e7%bc%96%e8%be%91%e8%b7%9d%e7%a6%bb","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=1054","title":{"rendered":"LeetCode 72 \u2013 \u7f16\u8f91\u8ddd\u79bb"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u4f60\u4e24\u4e2a\u5355\u8bcd word1 \u548c word2\uff0c \u8bf7\u8fd4\u56de\u5c06 word1 \u8f6c\u6362\u6210 word2 \u6240\u4f7f\u7528\u7684\u6700\u5c11\u64cd\u4f5c\u6570  \u3002<br \/>\n\u4f60\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u5355\u8bcd\u8fdb\u884c\u5982\u4e0b\u4e09\u79cd\u64cd\u4f5c\uff1a<br \/>\n\u63d2\u5165\u4e00\u4e2a\u5b57\u7b26<br \/>\n\u5220\u9664\u4e00\u4e2a\u5b57\u7b26<br \/>\n\u66ff\u6362\u4e00\u4e2a\u5b57\u7b26<\/p>\n<p>\u793a\u4f8b\uff1a<br \/>\n\u8f93\u5165\uff1aword1 = &#8220;horse&#8221;, word2 = &#8220;ros&#8221;<br \/>\n\u8f93\u51fa\uff1a3<br \/>\n\u89e3\u91ca\uff1a<br \/>\nhorse -> rorse (\u5c06 &#8216;h&#8217; \u66ff\u6362\u4e3a &#8216;r&#8217;)<br \/>\nrorse -> rose (\u5220\u9664 &#8216;r&#8217;)<br \/>\nrose -> ros (\u5220\u9664 &#8216;e&#8217;)<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u52a8\u6001\u89c4\u5212\uff0c\u8bbe dp[i][j] \u8868\u793a word1 \u7684\u524d i \u4f4d\u8f6c\u6362\u4e3a word2 \u7684\u524d j \u4f4d\u6240\u9700\u7684\u6700\u5c11\u64cd\u4f5c\u6570\u3002<br \/>\n&#8211; i \u548c j \u5b57\u7b26\u76f8\u540c\u65f6\uff0cdp[i][j] = dp[i-1][j-1]<br \/>\n&#8211; i \u548c j \u5b57\u7b26\u4e0d\u540c\u65f6\uff0cdp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1<\/p>\n<p>\u65f6\u95f4\u590d\u6742\u5ea6 O(m * n)\uff0cm \u548c n \u662f 2 \u4e2a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public int minDistance(String word1, String word2) {\n    int m = word1.length(), n = word2.length();\n    int[][] dp = new int[m + 1][n + 1];\n    for (int i = 0; i &lt;= m; i++) {\n        for (int j = 0; j &lt;= n; j++) {\n            if (i == 0 || j == 0) {\n                dp[i][j] = j == 0 ? i : j;\n            } else if (word1.charAt(i - 1) == word2.charAt(j - 1)) {\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])) + 1;\n            }\n        }\n    }\n    return dp[m][n];\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun minDistance(word1: String, word2: String): Int {\n    val m = word1.length\n    val n = word2.length\n    val dp = Array(m + 1) { IntArray(n + 1) }\n    for (i in 0..m) dp[i][0] = i\n    for (j in 0..n) dp[0][j] = j\n    for (i in 1..m) {\n        for (j in 1..n) {\n            if (word1[i - 1] == word2[j - 1]) {\n                dp[i][j] = dp[i - 1][j - 1]\n            } else {\n                dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1\n            }\n        }\n    }\n    return dp[m][n]\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u4f60\u4e24\u4e2a\u5355\u8bcd word1 \u548c word2\uff0c \u8bf7\u8fd4\u56de\u5c06 word1 \u8f6c\u6362\u6210 word2 \u6240\u4f7f\u7528\u7684\u6700\u5c11\u64cd [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[20,40,45],"class_list":["post-1054","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-40","tag-45"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1054","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1054"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1054\/revisions"}],"predecessor-version":[{"id":1055,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1054\/revisions\/1055"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}