{"id":3070,"date":"2020-02-27T15:31:23","date_gmt":"2020-02-27T07:31:23","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=3070"},"modified":"2020-02-27T15:31:23","modified_gmt":"2020-02-27T07:31:23","slug":"%e3%80%90leetcode%e3%80%910072-%e7%bc%96%e8%be%91%e8%b7%9d%e7%a6%bb","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/3070\/","title":{"rendered":"\u3010LeetCode\u3011*0072 \u7f16\u8f91\u8ddd\u79bb"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/edit-distance\/\">https:\/\/leetcode-cn.com\/problems\/edit-distance\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def minDistance(self, word1, word2):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> word1: str\n        <\/em><strong><em>:type<\/em><\/strong><em> word2: str\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: int\n        \"\"\"\n        <\/em>n = len(word1)\n        m = len(word2)\n        # if one of the strings is empty\n        if n * m == 0:\n            return n + m\n        # array to store the convertion history\n        d = [[0] * (m + 1) for _ in range(n + 1)]\n        # init boundaries\n        for i in range(n + 1):\n            d[i][0] = i\n        for j in range(m + 1):\n            d[0][j] = j\n        # DP compute\n        for i in range(1, n + 1):\n            for j in range(1, m + 1):\n                left = d[i - 1][j] + 1\n                down = d[i][j - 1] + 1\n                left_down = d[i - 1][j - 1]\n                if word1[i - 1] != word2[j - 1]:\n                    left_down += 1\n                d[i][j] = min(left, down, left_down)\n        return d[n][m]\nif __name__ == '__main__':\n    word1 = \"horse\"\n    word2 = \"ros\"\n    print(Solution().minDistance(word1, word2))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1adp\u89c4\u5219\u5982\u4e0b\uff1a<\/p>\n\n\n<ul><li> \u7b2c\u4e00\u4e2a\u5355\u8bcd\u7684\u524di\u4f4d\u53d8\u6210\u7b2c\u4e8c\u4e2a\u5355\u8bcd\u7684\u524dj-1\u4f4d\uff0c\u7136\u540e\u518d\u63d2\u5165\u4e00\u4e2a\u5b57\u7b26\uff08d[i][j-1]+1\uff09 <\/li><li> \u7b2c\u4e00\u4e2a\u5355\u8bcd\u7684\u524di-1\u4f4d\u53d8\u6210\u7b2c\u4e8c\u4e2a\u5355\u8bcd\u7684\u524dj\u4f4d\uff0c\u7136\u540e\u518d\u5220\u53bb\u4e00\u4e2a\u5b57\u7b26\uff08d[i-1][j]+1\uff09 <\/li><li> \u7b2c\u4e00\u4e2a\u5355\u8bcd\u7684\u524di-1\u4f4d\u53d8\u6210\u7b2c\u4e8c\u4e2a\u5355\u8bcd\u7684\u524dj-1\u4f4d\uff0c\u7136\u540e\u66ff\u6362\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u5982\u679c\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u76f8\u540c\uff0c\u5373\u7b2c\u4e00\u4e2a\u5355\u8bcd\u7684\u7b2ci\u4f4d\u548c\u7b2c\u4e8c\u4e2a\u5355\u8bcd\u7684\u7b2cj\u4f4d\u76f8\u540c\u7684\u8bdd\uff0c\u5c31\u4e0d\u7528\u66ff\u6362\u4e86\uff08d[i-1][j-1]\uff09\uff0c\u53cd\u4e4b\uff0c\u5982\u679c\u4e0d\u540c\u5c31\u66ff\u6362\u6700\u540e\u4e00\u4f4d\uff08d[i-1][j-1]+1\uff09 <\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/edit-distance\/ #  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":1973,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/3070"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=3070"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/3070\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=3070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=3070"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=3070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}