[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 ...
随机推荐
- JMeter学习笔记(九) 参数化3--User Defined Variables
3.User Defined Variables 1)添加用户定义的变量 2)添加变量 3)添加HTTP请求,引用变量,格式:${} 4)执行HTTP请求,察看结果树 5)用户定义的变量,优缺点: * ...
- 树莓派3_win10下使用"远程桌面连接"与树莓派通信(使用VNC实现连接后)
-----------------------------------------------------------学无止境------------------------------------- ...
- 问题 A: a+b
问题 A: a+b 时间限制: 1 Sec 内存限制: 32 MB提交: 285 解决: 124[提交][状态][讨论版][命题人:外部导入] 题目描述 实现一个加法器,使其能够输出a+b的值. ...
- LeetCode 36——有效的数独
1. 题目 2. 解答 将数独中数字的 ASCII 码值转化到 0-8 之间作为散列值,建立一个散列表,然后分别逐行.逐列.逐宫(3*3小块)统计每个数字的出现次数,若出现次数大于 1,则数独无效. ...
- 二分图的最大匹配——Hopcroft-Karp算法
http://blog.csdn.net/wall_f/article/details/8248373
- Web-request内置对象在JSP编程中的应用
- 【EasyNetQ】- 多态发布和订阅
您可以订阅接口,然后发布该接口的实现. 我们来看一个例子.我有一个接口IAnimal和两个实现Cat和Dog: public interface IAnimal { string Name { get ...
- 关于php网络爬虫phpspider
前几天,被老板拉去说要我去抓取大众点评某家店的数据,当然被我义正言辞的拒绝了,理由是我不会...但我的反抗并没有什么卵用,所以还是乖乖去查资料,因为我是从事php工作的,首先找的就是php的网络爬虫源 ...
- node和gulp实现前端工程自动化(附:gulp常用插件)
/** * 1. LESS编译 压缩 合并 * 2. JS合并 压缩 混淆 * 3. img复制 * 4. html压缩 */ // 在gulpfile中先载入gulp包,因为这个包提供了一些APIv ...
- Struts1之编码问题
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding=& ...