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 ...
随机推荐
- Java异常封装(自己定义错误码和描述,附源码)
真正工作了才发现,Java里面的异常在真正工作中使用还是十分普遍的.什么时候该抛出什么异常,这个是必须知道的. 当然真正工作里面主动抛出的异常都是经过分装过的,自己可以定义错误码和异常描述. 下面小宝 ...
- Hazelcast源码剖析之Eviction
v:* { } o:* { } w:* { } .shape { }p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-botto ...
- Dynamics CRM2016 Web API之Create related entities in one operation
本篇继续来介绍两个web api的接口,一个是"Create related entities in one operation"即在一步操作中完成主实体的创建加关联实体的创建,一 ...
- SceneKit:简单的3D游戏场景搭建
SceneKit是Apple用来开发休闲3D游戏的框架,不同于底层的OpenGL库,你仅仅需要很少的代码就可以快速看到实际的3D场景效果.下面简单的聊聊搭建一个3D游戏场景需要做的事情. 首先你必须用 ...
- SQLite 附加数据库(http://www.w3cschool.cc/sqlite/sqlite-attach-database.html)
SQLite 附加数据库 假设这样一种情况,当在同一时间有多个数据库可用,您想使用其中的任何一个.SQLite 的 ATTACH DTABASE 语句是用来选择一个特定的数据库,使用该命令后,所有的 ...
- 安卓自定义View实现图片上传进度显示(仿QQ)
首先看下我们想要实现的效果如下图(qq聊天中发送图片时的效果): 再看下图我们实现的效果: 实现原理很简单,首先我们上传图片时需要一个进度值progress,这个不管是自己写的上传的方法还是使用第三方 ...
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
1 PersonTestMapper.xml中的内容如下: <?xmlversion="1.0"encoding="UTF-8"?> < ...
- JSP标签JSTL(3)--迭代操作
简单点说,<c:forEach>标签的作用就是迭代输出标签内部的内容.它既可以进行固定次数的迭代输出,也可以依据集合中对象的个数来决定迭代的次数. <!--语法使用如下--> ...
- iOS中大流中的自定义cell 技术分享
AppDelegate.m指定根视图 self.window.rootViewController = [[UINavigationController alloc] initWithRootView ...
- Android使用SimpleAdapter
SimpleAdapter的使用步骤如下: 声明ListView,并进行初始化操作 准备数据集,一般用list来实现,当然也可以使用数组 为listview适配simpleadapter 如下代码: ...