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. python 之 决策树分类算法

    发现帮助新手入门机器学习的一篇好文,首先感谢博主!:用Python开始机器学习(2:决策树分类算法) J. Ross Quinlan在1975提出将信息熵的概念引入决策树的构建,这就是鼎鼎大名的ID3 ...

  2. tornado.ioloop.IOLoop相关文章

    http://6167018.blog.51cto.com/6157018/1532899 http://kenby.iteye.com/blog/1159621

  3. spark-submit 提交Application

    在spark1.0中推出spark-submit来统一提交applicaiton ./bin/spark-submit \ --class <main-class> --master &l ...

  4. 使用HTML引用标签来分隔Ticket回复

    今天在查看Ticket的时候,发现如何和客户之间有很多个来回,Ticket的Correspondence就会很长很长,虽然我们有自己的Breakline,但是由于客户回复邮件时,添加的用于分隔旧信息和 ...

  5. 布尔值运算&集合

    示例:返回booleanli = [] li = {} li = () if not li: print(1) radiansdict.has_key(key) #如果键在字典dict里返回true, ...

  6. js基础和运算符

    1.什么JavaScript?    运行环境 :    浏览器                    是一种具有安全性的客户端的脚本语言     用来实现与web页面交互 脚本语言:语言嵌入到htm ...

  7. Executor框架(五)Executors工厂类

    Executors 简介 Executors 是一个工厂类,其提供的是Executor.ExecutorService.ScheduledExecutorService.ThreadFactory 和 ...

  8. hive 锁

    HiveQL是一种SQL语言,但缺少udpate和insert类型操作时的行,列或者查询级别的锁支持,hadoop文件通常是一次写入(支持有限的文件追加功能),hadoop和hive都是多用户系统,锁 ...

  9. leetcode344

    public class Solution { public string ReverseString(string s) { var list = s.Reverse(); StringBuilde ...

  10. leetcode100

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...