72. Edit Distance (String; DP)
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
思路:动态规划。将所要求的min step作为状态,dp[i][j]表示word2的前j各字符通过word1的前i各字符转换最少需要多少步。可以看到有两个以上的string,通常状态要定义为二维数组,表示两个字符串前几个字符之间的关系。
class Solution {
public:
int minDistance(string word1, string word2) {
if(word1.length()==) return word2.length();
if(word2.length()==) return word1.length();
vector<vector<int>> dp(word1.length(), vector<int>(word2.length(),));
if(word1[]==word2[]) dp[][] = ;
else dp[][] = ;
for(int i = ; i < word1.length(); i++){
for(int j = ; j < word2.length(); j++){
if(i> && j>){
if(word1[i]==word2[j]){
dp[i][j] =min(min(dp[i-][j], dp[i][j-])+,dp[i-][j-]);
}
else{
dp[i][j]=min(min(dp[i-][j], dp[i][j-]),dp[i-][j-])+;
}
}
else if(i>){
if(word1[i]==word2[j]){
dp[i][j] =i;
}
else{
dp[i][j]=dp[i-][j]+;
}
}
else if(j>){
if(word1[i]==word2[j]){
dp[i][j] =j;
}
else{
dp[i][j]=dp[i][j-]+;
}
}
}
}
return dp[word1.length()-][word2.length()-];
}
};
72. Edit Distance (String; DP)的更多相关文章
- 【Leetcode】72 Edit Distance
72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...
- 刷题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 ...
- 72. Edit Distance
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
- [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(困难,确实挺难的,但很经典,双序列DP问题)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 第十八周 Leetcode 72. Edit Distance(HARD) O(N^2)DP
Leetcode72 看起来比较棘手的一道题(列DP方程还是要大胆猜想..) DP方程该怎么列呢? dp[i][j]表示字符串a[0....i-1]转化为b[0....j-1]的最少距离 转移方程分三 ...
- [leetcode DP]72. Edit Distance
计算最少用多少不把word1变为word2, 思路:建立一个dp表,行为word1的长度,宽为word2的长度 1.边界条件,dp[i][0] = i,dp[0][j]=j 2.最优子问题,考虑已经知 ...
随机推荐
- shell 1基础
shell简介 shell是一个用C语言编写的程序,是用户使用Linux的桥梁.shell既是一种命令语言,又是一种程序设计语言. shell脚本(shell script),是一种为shell编写的 ...
- Eclipse里面的Maven项目如果下载依赖的jar包的源码
Window---------Properties---------------Maven--------------勾选Download Artifact Sources和Download Arti ...
- golang web框架 beego 学习 (一) 环境搭建
下面的命令我都是在$GOPATH的路径下执行的: 1. 首先下载beego框架: go get github.com/astaxie/beego (注意:运行上面命令时没有反应,需要在etc/host ...
- 第7章 进程关系(5)_贯穿案例2:mini shell(2)
5. 贯穿案例2:mini shell(2) (1)己经完成的功能:pwd.cd.exit命令 (2)阶段性目标: ①env.export.echo及其他命令 ②标准输入.输出重定向"> ...
- redis windows dll 下载
https://pecl.php.net/package/redis http://blog.csdn.net/leesin2011/article/details/72801629 http://w ...
- AFNetworkingErrorDomain 错误解决方法
首先我们来看一下错误信息: Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299 ...
- smfony设置量表之间的关系
设置量表之间的关系 验证是否ok 查看我们定义是否有问题 数据库操作 http://www.2cto.com/database/201504/387197.html 设置时间段数据库自动插入时间 不 ...
- easyui datagrid列使用按钮的一些心得(转)
http://blog.csdn.net/sskicgah/article/details/16939959 以前,用easyui的datagrid,有时候会用到一些操作选项,比如代码如下: $('# ...
- 18. 进livebos对象直接跳转jsp页面的做法
在网格脚本定义添加: window.onload=function(){ window.location.href='/plug-in/sinopec/contractManagement/h ...
- php 面试考点总结-高并发和大流量解决方案考点
1.web资源防盗链 盗链概念 盗链是指在自己的页面上展示一些并不在自己服务器的内容 防盗链工作原理 通过referer或者签名,网站可以检测目标网页访问的来源页,一旦检测到来源页不是本站即进行阻止或 ...