LeetCode之“动态规划”:Edit Distance
题目要求:
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
该题的解析宅自一博文:
我们维护的变量res[i][j]表示的是word1的前i个字符和word2的前j个字符编辑的最少操作数是多少。假设我们拥有res[i][j]前的所有历史信息,看看如何在常量时间内得到当前的res[i][j],我们讨论两种情况:
1)如果word1[i-1]=word2[j-1],也就是当前两个字符相同,也就是不需要编辑,那么很容易得到res[i][j]=res[i-1][j-1],因为新加入的字符不用编辑;
2)如果word1[i-1]!=word2[j-1],那么我们就考虑三种操作,如果是插入word1,那么res[i][j]=res[i-1][j]+1,也就是取word1前i-1个字符和word2前j个字符的最好结果,然后添加一个插入操作;如果是插入word2,那么res[i][j]=res[i][j-1]+1,道理同上面一种操作;如果是替换操作,那么类似于上面第一种情况,但是要加一个替换操作(因为word1[i-1]和word2[j-1]不相等),所以递推式是res[i][j]=res[i-1][j-1]+1。上面列举的情况包含了所有可能性,有朋友可能会说为什么没有删除操作,其实这里添加一个插入操作永远能得到与一个删除操作相同的效果,所以删除不会使最少操作数变得更好,因此如果我们是正向考虑,则不需要删除操作。取上面几种情况最小的操作数,即为第二种情况的结果,即res[i][j] = min(res[i-1][j], res[i][j-1], res[i-1][j-1])+1。
程序大概流程图如下:

程序如下:
class Solution {
public:
int min(int a, int b, int c)
{
int tmp = (a < b) ? a : b;
return (tmp < c) ? tmp : c;
}
int minDistance(string word1, string word2) {
int szWord1 = word1.size();
int szWord2 = word2.size();
if(szWord1 == )
return szWord2;
if(szWord2 == )
return szWord1;
vector<vector<int> > dp(szWord1 + , vector<int>(szWord2 + , ));
for(int i = ; i < szWord1 + ; i++)
dp[i][] = i;
for(int j = ; j < szWord2 + ; j++)
dp[][j] = j;
for(int i = ; i < szWord1 + ; i++)
for(int j = ; j < szWord2 + ; j++)
{
if(word1[i-] == word2[j-])
dp[i][j] = dp[i-][j-];
else
dp[i][j] = min(dp[i-][j], dp[i][j-], dp[i-][j-]) + ;
}
return dp[szWord1][szWord2];
}
};
LeetCode之“动态规划”:Edit Distance的更多相关文章
- [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 ...
- 【Leetcode】72 Edit Distance
72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...
- [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 ...
- ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java
Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...
- [LeetCode#161] One Edit Distance
Problem: Given two strings S and T, determine if they are both one edit distance apart. General Anal ...
- 【一天一道LeetCode】#72. Edit Distance
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- (LeetCode 72)Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 【LeetCode】72. Edit Distance 编辑距离(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...
- leetcode@ [72/115] Edit Distance & Distinct Subsequences (Dynamic Programming)
https://leetcode.com/problems/edit-distance/ Given two words word1 and word2, find the minimum numbe ...
- [Leetcode 72]编辑距离 Edit Distance
[题目] Given two words word1 and word2, find the minimum number of operations required to convert word ...
随机推荐
- 从1....n中随机输出m个不重复的数
void knuth(int n, int m) { srand((unsigned) time( NULL)); for (int i = 0; i < n && m; i++ ...
- [Vim]vim使用笔记--分屏操作
我们经常要打开多个文件,不同的窗口操作多个文件,分屏就很好用了. 1 命令模式下: :new,新建文件并分屏, 快捷键,Ctrl+W,然后马上按n键 :spilt 水平分屏,将当前屏分为两个,水平的. ...
- ROS_Kinetic_x 目前已更新的常用機器人資料 rosbridge agvs pioneer_teleop nao TurtleBot
Running Rosbridge Description: This tutorial shows you how to launch a rosbridge server and talk to ...
- Java提升篇之反射的原理(二)
Java提升篇之通过反射越过泛型检查 /* *问题:在一个ArrayList<Integer>对象中,在这个集合中添加一个字符串. */ 在我们还没有学反射前,遇到这个问题都是无法实现的, ...
- Spark发展现状与战线
前言 现今Spark正是风头正劲时,Spark本是UCBerkeley的AMPLab诞生的项目,后来捐赠给了Apache来管理源码和后续发展.今年从Apache孵化器终于孵化出了1.0版本.其对大数据 ...
- picasso图片缓存框架
picasso是Square公司开源的一个Android图形缓存库,地址http://square.github.io/picasso/,可以实现图片下载和缓存功能. picasso使用简单,如下 [ ...
- J2EE进阶(八)Hibernate与延迟加载机制探究
Hibernate与延迟加载机制探究 前言 Hibernate对象关系映射提供延迟的与非延迟的对象初始化.非延迟加载在读取一个对象的时候会将与这个对象所有相关的其他对象一起读取出来.这有时会导致成百的 ...
- UNIX环境高级编程——网络编程常用函数及结构
IP地址的转换 #include <arpa/inet.h> int inet_aton(const char *strptr, struct i ...
- 《java入门第一季》之TreeSet存储自定义对象并保证排序和唯一
上一篇用一个简单的例子,介绍了treeset集合存储的内部过程,这里再完善其存储自定义对象保证唯一. 需求:A: * 自然排序,按照年龄从小到大排序 * B: * 成员变量值都相 ...
- 偏置方差分解Bias-variance Decomposition
http://blog.csdn.net/pipisorry/article/details/50638749 偏置-方差分解(Bias-Variance Decomposition) 偏置-方差分解 ...