[Leetcode Week8]Edit Distance
Edit Distance 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/edit-distance/description/
Description
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
Solution
class Solution {
public:
int minDistance(string word1, string word2) {
if (word1 == word2)
return 0;
if (word1.empty())
return word2.length();
if (word2.empty())
return word1.length();
int len1 = word1.length() + 1;
int len2 = word2.length() + 1;
int** f = new int*[len1];
int i, j;
for (i = 0; i < len1; i++) {
f[i] = new int[len2];
f[i][0] = i;
}
for (j = 0; j < len2; j++) {
f[0][j] = j;
}
for (i = 1; i < len1; i++) {
for (j = 1; j < len2; j++) {
if (word1[i - 1] == word2[j - 1]) {
f[i][j] = f[i - 1][j - 1];
} else {
f[i][j] = min(min(f[i - 1][j] + 1, f[i][j - 1] + 1), f[i - 1][j - 1] + 1);
}
}
}
int res = f[len1 - 1][len2 - 1];
for (i = 0; i < len1; i++)
delete [] f[i];
delete [] f;
return res;
}
};
解题描述
这道题是动态规划中经典的编辑距离问题,关键之处在于将求算总的编辑的距离这个大问题转换成每一步比较两个字符串中指定位置上的字符的时候应该得到的编辑距离f[i][j]
。增加、删除、替换都是相对上一步编辑距离+1,那关键就是上一步应该选择哪一步?很明显就是选择之前的编辑距离最少的一步,即f[i][j] = min(min(f[i - 1][j] + 1, f[i][j - 1] + 1), f[i - 1][j - 1] + 1)
的意义;如果指定位上的字符相等,那显然就有f[i][j] = f[i - 1][j - 1]
。
[Leetcode Week8]Edit Distance的更多相关文章
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- Java for LeetCode 072 Edit Distance【HARD】
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- LeetCode One Edit Distance
原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if the ...
- [LeetCode] 72. Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- 【leetcode】Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- leetCode 72.Edit Distance (编辑距离) 解题思路和方法
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- [LeetCode] 72. Edit Distance(最短编辑距离)
传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...
- LeetCode - 72. Edit Distance
最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...
- 【leetcode】Edit Distance (hard)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
随机推荐
- Windows下nginx作为静态资源服务器使用
一.Nginx下载与安装 1.nginx官方下载地址:http://nginx.org/ 2.下载完后将压缩包解压即可 3.nginx配置文件为根目录下conf\nginx.conf 二.Nginx常 ...
- Python-类-函数参数-takes 0 positional arguments but 1 was given
在学习Python基础的时候,在创建某一个shownametest()函数,解析器会报错 TypeError: shownametest() takes 0 positional arguments ...
- 大批量复制Oracle数据表,连带复制主键约束,字段说明以及字段默认值(量产)
DECLARE CURSOR tab_name_cur IS SELECT table_name FROM user_tables ...
- HDU 4714 Tree2cycle(树状DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup)
Description A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 ...
- Unity UGUI 图片 轴对称效果 减少资源
制作UI的过程中,为了节省资源,对称的图一般美术切一半给我们 手动拼图 有时会出现拼接处出现裂缝或重叠 调整大小时也不方便 得一块一块调整 所以就用BaseMeshEffect 的ModifyMesh ...
- Java FTP下载文件以及编码问题小结
问题 之前在开发过程中,遇到了一点问题,我要访问一个FTP服务器去下载文件详细情况如下: 1. 需要传入一个可能为中文的文件名: 2. 通过文件名去FTP上寻找该文件: 3. FTP服务器的命名编码为 ...
- OpenCV中的按钮问题
在HighGUI中,没有显示提供任何形式的按钮.一般有两种方法替代: 1.用只有两个状态的滑动条来替代按钮.开关(switch)事实上就是只有两个状态的滑动条,这两个状态是on和off.然后通过回调函 ...
- 结对作业 -GUI四则运算
目录: 一.前言(及项目地址) 二.PSP(planning) 三.结对编程中对接口的设计 四.计算模块接口的设计与实现过程 五.计算模块接口部分的性能改进 六.计算模块部分单元测试展示 七.计算模块 ...
- PokeCats开发者日志(一)
现在是PokeCats游戏开发的第三天的上午,突然心血来潮想记录一下这个开发过程,于是写起了开发者日志. day1 作为一只ACM退役喵,寒假回家,闲着没事,天天在召唤师峡谷里闲逛也挺没意思的 ...
- [Elasticsearch] 多字段搜索 (一) - 多个及单个查询字符串
多字段搜索(Multifield Search) 本文翻译自官方指南的Multifield Search一章. 查询很少是只拥有一个match查询子句的查询.我们经常需要对一个或者多个字段使用相同或者 ...