美文网首页
72. Edit Distance [Hard] 编辑距离DP

72. Edit Distance [Hard] 编辑距离DP

作者: 一个想当大佬的菜鸡 | 来源:发表于2019-06-08 07:57 被阅读0次
    72. Edit Distance
    class Solution(object):
        def minDistance(self, word1, word2):
            """
            :type word1: str
            :type word2: str
            :rtype: int
            """
            m, n = len(word1), len(word2)
            dp = [[0 for i in range(n+1)] for j in range(m+1)]
            for i in range(m+1):
                dp[i][0] = i
            for i in range(n+1):
                dp[0][i] = i
            for i in range(1, m+1):
                for j in range(1, n+1):
                    if word1[i-1]==word2[j-1]:
                        dp[i][j] = dp[i-1][j-1]
                    else:
                        dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1]+1)
            return dp[-1][-1]
    

    相关文章

      网友评论

          本文标题:72. Edit Distance [Hard] 编辑距离DP

          本文链接:https://www.haomeiwen.com/subject/zzbbxctx.html