码迷,mamicode.com
首页 > 编程语言 > 详细

Edit Distance @Leetcode -- Python

时间:2014-05-09 04:19:03      阅读:407      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   

http://oj.leetcode.com/problems/edit-distance/

bubuko.com,布布扣
class Solution:
    # @return an integer
    def minDistance(self, word1, word2):
        len1 = len(word1)
        len2 = len(word2)
        dp = [[0 for j in xrange(len2+1)] for i in xrange(len1 +1)]
        for i in xrange(len1+1):
            dp[i][0] = i
        for j in xrange(len2+1):
            dp[0][j] = j
        for i in xrange(1, len1+1):
            for j in xrange(1, len2+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],dp[i-1][j]) + 1
        return dp[len1][len2]
bubuko.com,布布扣

 

Edit Distance @Leetcode -- Python,布布扣,bubuko.com

Edit Distance @Leetcode -- Python

标签:style   blog   class   code   java   tar   

原文地址:http://www.cnblogs.com/sumnous/p/3716290.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!