[leetcode DP]72. Edit Distance
计算最少用多少不把word1变为word2,
思路:建立一个dp表,行为word1的长度,宽为word2的长度
1.边界条件,dp[i][0] = i,dp[0][j]=j
2.最优子问题,考虑已经知道word1[0:i-1]转变为word2[0:j-1]的次数,只需要考虑word1[i]和word[j]的情况
3.子问题重叠,word1[i]和word2[j]是否相等,每种情况下怎样有最少步骤
class Solution(object):
def minDistance(self, word1, word2):
d1,d2 = len(word1),len(word2)
dp = [[0 for j in range(d2+1)] for i in range(d1+1)]
for i in range(d1+1):
dp[i][0] = i
for j in range(d2+1):
dp[0][j] = j
for i in range(1,d1+1):
for j in range(1,d2+1):
if word1[i-1] == word2[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = min(dp[i][j-1],dp[i-1][j-1],dp[i-1][j])+1
return dp[d1][d2]
[leetcode DP]72. Edit Distance的更多相关文章
- 【Leetcode】72 Edit Distance
72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...
- 【一天一道LeetCode】#72. Edit Distance
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- 【LeetCode】72. Edit Distance 编辑距离(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...
- [leetcode]161. One Edit Distance编辑步数为一
Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...
- 刷题72. Edit Distance
一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一 ...
- [LeetCode] 72. Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- [LeetCode] 72. Edit Distance(最短编辑距离)
传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...
- leetCode 72.Edit Distance (编辑距离) 解题思路和方法
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- 72. Edit Distance
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
随机推荐
- 读懂复杂C声明的黄金法则
在网上遇见felix,他让我读 http://www.felix021.com/blog/read.php?2072,读完之后觉得收获很大,需要练习一下. 黄金法则:从声明的变量开始,先向右看,再向左 ...
- bzoj 3790 神奇项链(Manacher,DP+BIT | 贪心)
[题意] 你可以产生一个回文串,也可以将两个串合并成一个串,问产生目标串需要的最少合并次数. [思路] 显然我们要先产生目标串中包含的极大回文字符串. Manacher求出每个位置可以向两边延伸的最长 ...
- HDU 2058 The sum problem 数学题
解题报告:可以说是一个纯数学题,要用到二元一次和二元二次解方程,我们假设[a,b]这个区间的所有的数的和是N,由此,我们可以得到以下公式: (b-a+1)*(a+b) / 2 = N;很显然,这是一个 ...
- python正则表达式-re模块的爱恨情仇
利用python的re模块,使用正则表达式对字符串进行处理 # 编辑者:闫龙 import re restr = "abccgccc123def456ghi789jgkl186000&quo ...
- 南京邮电大学 CTF 逆向部分 Writeup
Hello,RE! 提示 IDA 中按 R . Google 到 IDA 中 R 快捷键是 Character ,转为字符串. 丢进 IDA(虽然我并不会使用 IDA 有个 strcmp 函数,比较 ...
- Anaconda 安装tensorflow(GPU)
1.安装 如果是安装CPU模式的tensorflow,只要输入一下代码就可以了 pip3 install tensorflow #python3pip install tensorflow #pyth ...
- USB设备被识别流程【转】
转自:http://blog.csdn.net/myarrow/article/details/8286876 USB模块包括usb core,host,hub,device驱动,其中hub会启动一个 ...
- 博客转移至github
博客转移到github 鉴于github的各种优势,博客转移!
- Python_oldboy_自动化运维之路(四)
本节内容 集合 字符编码与转码 函数语法及基本特性 函数参数与局部变量 返回值和嵌套函数 递归 匿名函数 高阶函数 1.集合 集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变 ...
- Mat结构
主要是记录一下大牛的博客,再次感谢这些无私的博主. 这篇博客http://blog.csdn.net/yang_xian521/article/details/7107786中,我觉得要注意的是Mat ...