{"id":3095,"date":"2022-05-05T21:26:30","date_gmt":"2022-05-05T13:26:30","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=3095"},"modified":"2022-05-05T21:26:30","modified_gmt":"2022-05-05T13:26:30","slug":"%e7%ae%97%e6%b3%95%e5%9f%ba%e7%a1%80%e7%ac%ac18%e5%a4%a9","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=3095","title":{"rendered":"\u7b97\u6cd5\u57fa\u7840\u7b2c18\u5929"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode-cn.com\/problems\/edit-distance\/\">72. \u7f16\u8f91\u8ddd\u79bb<\/a><\/h4>\n\n\n\n<p><a href=\"https:\/\/www.zeroheart.xyz\/wordpress\/?p=3092\">\u7b97\u6cd5\u57fa\u7840\u7b2c17\u5929 \u2013 zeroheart<\/a>\uff0c\u548c\u8fd9\u4e00\u9898\u7684\u533a\u522b\u662f\u4e00\u4e2a\u53ef\u4ee5\u6539\u52a8\u4e24\u4e2a\u4e32\uff0c\u4e00\u4e2a\u53ea\u80fd\u52a8\u4e00\u4e2a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><br><em>\/**<br><\/em><em> 72. \u7f16\u8f91\u8ddd\u79bb<br><\/em><em> \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><\/em><em><br><\/em><em> \u4f60\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u5355\u8bcd\u8fdb\u884c\u5982\u4e0b\u4e09\u79cd\u64cd\u4f5c\uff1a<br><\/em><em><br><\/em><em> \u63d2\u5165\u4e00\u4e2a\u5b57\u7b26<br><\/em><em> \u5220\u9664\u4e00\u4e2a\u5b57\u7b26<br><\/em><em> \u66ff\u6362\u4e00\u4e2a\u5b57\u7b26<br><\/em><em><br><\/em><em><br><\/em><em> \u793a\u4f8b 1\uff1a<br><\/em><em><br><\/em><em> \u8f93\u5165\uff1aword1 = \"horse\", word2 = \"ros\"<br><\/em><em> \u8f93\u51fa\uff1a3<br><\/em><em> \u89e3\u91ca\uff1a<br><\/em><em> horse -&gt; rorse (\u5c06 'h' \u66ff\u6362\u4e3a 'r')<br><\/em><em> rorse -&gt; rose (\u5220\u9664 'r')<br><\/em><em> rose -&gt; ros (\u5220\u9664 'e')<br><\/em><em> \u793a\u4f8b 2\uff1a<br><\/em><em><br><\/em><em> \u8f93\u5165\uff1aword1 = \"intention\", word2 = \"execution\"<br><\/em><em> \u8f93\u51fa\uff1a5<br><\/em><em> \u89e3\u91ca\uff1a<br><\/em><em> intention -&gt; inention (\u5220\u9664 't')<br><\/em><em> inention -&gt; enention (\u5c06 'i' \u66ff\u6362\u4e3a 'e')<br><\/em><em> enention -&gt; exention (\u5c06 'n' \u66ff\u6362\u4e3a 'x')<br><\/em><em> exention -&gt; exection (\u5c06 'n' \u66ff\u6362\u4e3a 'c')<br><\/em><em> exection -&gt; execution (\u63d2\u5165 'u')<br><\/em><em><br><\/em><em><br><\/em><em> \u63d0\u793a\uff1a<br><\/em><em><br><\/em><em> 0 &lt;= word1.length, word2.length &lt;= 500<br><\/em><em> word1 \u548c word2 \u7531\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210<br><\/em><em><br><\/em><em> * <\/em><strong><em>@author <\/em><\/strong><em>zeroheart<br><\/em><em> *<br><\/em><em> *\/<br><\/em>public class Question72 {<br>    \/\/ \u8fd9\u9898\u53ea\u80fd\u6539\u53d8\u4e00\u4e2a\u5b57\u7b26\u4e32<br>    public int minDistance(String word1, String word2) {<br>        \/\/ dp[i][j] \u8868\u793a0-i, 0-j \u76f8\u540c\u7684\u6700\u5c0f\u8ddd\u79bb<br><br>        int length = word1.length();<br>        int length2 = word2.length();<br>        int [][] dp = new int[length+1][length2+1];<br><br>        for(int i = 0; i&lt;=length; i++){<br>            dp[i][0] = i; \/\/ \u5220\u9664\u6240\u6709\u5143\u7d20\uff0c\u4e0e\u7a7a\u4e32\u76f8\u540c<br>        }<br>        for(int j = 0; j&lt;=length2; j++){<br>            dp[0][j] = j; \/\/ \u5220\u9664\u6240\u6709\u5143\u7d20\uff0c\u4e0e\u7a7a\u4e32\u76f8\u540c<br>        }<br><br>        for(int i = 1; i&lt;=length; i++){<br>            for(int j = 1; j&lt;=length2; j++){<br>                if(word1.charAt(i-1) == word2.charAt(j-1)){<br>                    dp[i][j] = dp[i-1][j-1];<br>                }else {<br>\/\/                    dp[i][j] = Math.min(dp[i-1][j-1]+1, Math.min(dp[i-1][j]+1, dp[i][j-1]+1));<br>                    dp[i][j] = 1 + Math.<em>min<\/em>(dp[i-1][j-1], Math.<em>min<\/em>(dp[i-1][j], dp[i][j-1]));<br>                }<br>            }<br>        }<br><br>        return dp[length][length2];<br>    }<br><br>    public static void main(String[] args) {<br>    }<br><br><br><br><br>}<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>72. \u7f16\u8f91\u8ddd\u79bb \u7b97\u6cd5\u57fa\u7840\u7b2c17\u5929 \u2013 zeroheart\uff0c\u548c\u8fd9\u4e00\u9898\u7684\u533a\u522b\u662f\u4e00\u4e2a\u53ef\u4ee5\u6539\u52a8\u4e24\u4e2a\u4e32\uff0c\u4e00\u4e2a\u53ea\u80fd\u52a8\u4e00\u4e2a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[155],"tags":[158],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3095"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3095"}],"version-history":[{"count":2,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3095\/revisions"}],"predecessor-version":[{"id":3097,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3095\/revisions\/3097"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}