LeetCode 72. Edit Distance-----Python实现
2018-01-24 00:18
459 查看
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
a) Insert a character
b) Delete a character
c) Replace a character
这题是典型的动态规划算法实现。主要是要找出递推关系式。第一种方法是ans[i][j]表示从word1[0~i]和word[0~j]相等时操作的步骤.
所以当 word1[i-1] == word2[j-1]时ans[i][j] = ans[i-1][j-1],否则ans[i][j] = min(ans[i-1][j],ans[i][j-1],ans[i-1][j-1])+1。但要注意ans的初始化,即第一行和第一列的意义。具体意义代码中有解释。
<
9caa
div>
You have the following 3 operations permitted on a word:
a) Insert a character
b) Delete a character
c) Replace a character
这题是典型的动态规划算法实现。主要是要找出递推关系式。第一种方法是ans[i][j]表示从word1[0~i]和word[0~j]相等时操作的步骤.
所以当 word1[i-1] == word2[j-1]时ans[i][j] = ans[i-1][j-1],否则ans[i][j] = min(ans[i-1][j],ans[i][j-1],ans[i-1][j-1])+1。但要注意ans的初始化,即第一行和第一列的意义。具体意义代码中有解释。
<
9caa
div>
class Solution(object): def minDistance(self, word1, word2): """ :type word1: str :type word2: str :rtype: int """ # inf=sys.maxint#import sys #可以不用最大值啊,因为第一行,第一列都有赋值了,所以后面的值都会更新 m,n=len(word1),len(word2) #m列,n行 ans=[[0 for i in range(n+1)] for j in range(m+1)] #ans[i][j]表示从word1[i]和word[j]相等时操作的步骤,用0就行,不用maxint # ans[0][0]=0 可以不要啊,后面循环里会包括它啊 for i in range(m+1): for j in range(n+1): if i == 0 : ans[i][j]=j #word1 中的0个变成word2中的j个,所以只能是不断的insert,即为不断地加j个,是j个就要加上j个啊 #相当于第一行赋值为0,1,2,。。。j if j == 0: ans[i][j]=i#word1 中的i个变成word2中的0个,所以只能是不断的delete,即为不断地删除i个,是j个就要加上i个啊 #相当于第一列赋值为0,1,2,。。。i if j > 0 and i > 0: if word1[i-1] == word2[j-1]: #ans[i][j] 比word1和word2中对应的字符串的i,j要大一啊 ans[i][j] = ans[i-1][j-1] else: ans[i][j] = min(ans[i-1][j],ans[i][j-1],ans[i-1][j-1])+1 #不要忘了加1啊 return ans[m]第二种算法就是ans[i][j]表示的是word1[i:]和word[j:]相等时操作的步骤.相当于从尾开始匹配,不好理解啊。具体代码如下,h
class Solution(object): def minDistance(self, word1, word2): """ :type word1: str :type word2: str :rtype: int """ m,n = len(word1),len(word2) ans = [[0 for i in range(n + 1)] for j in range(m + 1)] for i in range(m + 1): ans[i] = m - i for i in range(n + 1): ans[m][i] = n - i m -= 1;n -= 1 while m >= 0: t = n while t >= 0: if word1[m] == word2[t]: ans[m][t] = ans[m + 1][t + 1] else: ans[m][t] = min(ans[m][t+1],ans[m+1][t],ans[m+1][t+1]) + 1 t -= 1 m -= 1 return ans[0][0]
相关文章推荐
- [Leetcode]@python 72. Edit Distance
- leetcode 461. Hamming Distance python实现
- 【contains-duplicate】leetCode python实现
- LeetCode 86 Partition List (Python详解及实现)
- [leetcode, python] Total Hamming Distance 多个数字之间的汉明距离
- LeetCode 72. Edit Distance
- leetcode 【 Merge k Sorted Lists 】python 实现
- python实现 LeetCode37——Sudoku Solver
- LeetCode 72. Edit Distance
- [leetcode]53. Maximum Subarray 最大连续子串python实现【medium】
- LeetCode 72. Edit Distance
- LeetCode刷题 | 72. Edit Distance 难题 编辑距离dp
- 【leetcode】 46. Permutations ,python实现
- LeetCode 56 Merge Intervals(Python实现及详解)
- LeetCode专题-Python实现之第28题: Implement strStr()
- 每日一题 LeetCode 有效的数字 Python实现
- leetcode 【 Insertion Sort List 】 python 实现
- [leetcode]Edit Distance @ Python
- LeetCode 80 Remove Duplicates from Sorted Array II (Python详解及实现)
- 【leetcode】Integer to Roman--python实现