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)的更多相关文章

  1. 【Leetcode】72 Edit Distance

    72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...

  2. 刷题72. Edit Distance

    一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一 ...

  3. [LeetCode] 72. Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to  ...

  4. 72. Edit Distance

    题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...

  5. [LeetCode] 72. Edit Distance(最短编辑距离)

    传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...

  6. leetCode 72.Edit Distance (编辑距离) 解题思路和方法

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  7. 72. Edit Distance(困难,确实挺难的,但很经典,双序列DP问题)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  8. 第十八周 Leetcode 72. Edit Distance(HARD) O(N^2)DP

    Leetcode72 看起来比较棘手的一道题(列DP方程还是要大胆猜想..) DP方程该怎么列呢? dp[i][j]表示字符串a[0....i-1]转化为b[0....j-1]的最少距离 转移方程分三 ...

  9. [leetcode DP]72. Edit Distance

    计算最少用多少不把word1变为word2, 思路:建立一个dp表,行为word1的长度,宽为word2的长度 1.边界条件,dp[i][0] = i,dp[0][j]=j 2.最优子问题,考虑已经知 ...

随机推荐

  1. 本地ip 和 网络ip 解释

    本地IP其实就是私有IP地址10.0.0.0--10.255.255.255172.16.0.0----172.31.255.255192.168.0.0---192.168.255.255 这些都是 ...

  2. 【转】SQL模糊查询

    在进行数据库查询时,有完整查询和模糊查询之分. 一般模糊查询语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: 1,% :表示任 ...

  3. jquery knob旋钮插件

    <!DOCTYPE html> <html> <head> <title>jQuery Knob 尝试</title> <script ...

  4. 关于生成器---(yield)

    生成器:是自定义的迭代器(自己用python代码写的迭代器),函数中见到yield的就是生成器 那么yield前后的变量又该怎么理解 看例子一 def counter(name): print('%s ...

  5. 代码: 日期和时间 datepicker

    bootstrap 的相关的时间插件 http://www.bootcss.com/p/bootstrap-datetimepicker/ jquery ui的日期插件 http://www.w3cs ...

  6. C# WebBrowser

    WebBrowser中的按钮调用WinForm中的事件: private void Form1_Load(object sender, EventArgs e) { this.webBrowser1. ...

  7. scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class

    转自:https://blog.csdn.net/lzj0470/article/details/17786587 quartz版本号:2.1.7 错误: Caused by: <a href= ...

  8. django celery 定时任务

    可参考上一篇:http://www.cnblogs.com/wumingxiaoyao/p/8515075.html 1. 安装 django-celery-beat pip3 install dja ...

  9. MyEclipse: Java代码与UML自动转换

    第一步:新建UML2 第二步:拖拽左边的代码向右侧

  10. rssh RSA(非对称密钥)

    rssh ,非对称密钥,分为密钥和公钥 ,密钥在对面机器,需要进入的文件中,公钥是放在本地机器上 import paramiko private_key = paramiko.RSAKey.from_ ...