刷题72. Edit Distance
一、题目说明
题目72. Edit Distance,计算将word1转换为word2最少需要的操作。操作包含:插入一个字符,删除一个字符,替换一个字符。本题难度为Hard!
二、我的解答
这个题目一点思路也没,就直接看答案了。用的还是dp算法,dp[n1+1][n2+1]中的dp[i][j]表示将word1的前i位,变为word2的前j位需要的步骤。注意第1行是空,第1列也是空。

1.第一行中,dp[0][i]表示空字符""到word2[0,...,i]需要编辑几次
2.第一列中,dp[i][0]表示空字符到word2[0,...,i]需要编辑几次
3.循环计算dp的值
if(word1[i]==word2[j]){
dp[i][j] == dp[i-1][j-1]
}else{
dp[i][j]=min(dp[i−1][j−1],dp[i][j−1],dp[i−1][j])+1
}
有了方法,实现不难:
class Solution{
public:
int minDistance(string word1,string word2){
int n1 = word1.size(),n2= word2.size();
if(n1<=0) return n2;
if(n2<=0) return n1;
vector<vector<int>> dp(n1+1,vector<int>(n2+1,0));
//初始化第1行
for(int i=0;i<=n2;i++){
dp[0][i] = i;
}
//初始化第1列
for(int i=0;i<=n1;i++){
dp[i][0] = i;
}
//计算dp矩阵
// if(word1[i]==word2[j]){
// dp[i][j] == dp[i-1][j-1]
// }else{
// dp[i][j]=min(dp[i-1][j-1],dp[i][j-1],dp[i-1][j])+1
// }
for(int i=1;i<=n1;i++){//行
for(int j=1;j<=n2;j++){//列
if(word1[i-1]==word2[j-1]) {
dp[i][j] = dp[i-1][j-1];
}else{
dp[i][j] = min(min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1])+1;
}
}
}
return dp[n1][n2];
}
};
性能如下:
Runtime: 16 ms, faster than 40.38% of C++ online submissions for Edit Distance.
Memory Usage: 11.3 MB, less than 62.50% of C++ online submissions for Edit Distance.
三、优化措施
今天做这么多吧,有点晕了。明天继续!
再回头看看题目Edit Distance,我好像以前做过,不过忘记了。
刷题72. Edit Distance的更多相关文章
- LintCode刷题笔记-- Edit distance
标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...
- 【Leetcode】72 Edit Distance
72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...
- 72. Edit Distance
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
- [LeetCode] 72. Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- 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 ...
- 72. Edit Distance(困难,确实挺难的,但很经典,双序列DP问题)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- [leetcode]72. Edit Distance 最少编辑步数
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
随机推荐
- Javascript的重要数据类型-对象
这次的分享,主要还是想跟大家聊聊Javascript语言中很重要的概念之一,对象.为什么说之一呢?因为Javascript其他重要概念还包括:作用域 作用域链 继承 闭包 函数 继承 数组 ..... ...
- 使用nohup不产生log文件方法
思想 无法阻止nohup产生日志可以将其定向到空文件实现 实现 $ nohup xxx >/dev/null 2>&1 &
- USACO06DEC 牛奶模式
题意:求最长的可重叠的 K重复子串 的长度 考虑二分长度s,转化为验证性问题. 对SA进行分组.保证组内Height最小为s.这样在组内RMQ就可以任意了,因为RMQ一定是大于S的. 只要组内元素个数 ...
- navicat 连接报2059错误
原因 navicat不支持mysql新版本的加密规则,mysql8 之前的版本中加密规则是mysql_native_password, mysql8之后,加密规则是caching_sha2_passw ...
- 搭建 Review Board - SVN 审核工具
一.安装环境 CentOS-6.7,ReviewBoard-2.5.1.1 二.安装环境的配置 1.确认当前系统中有如下包,若没有,使用yum安装 httpd-2.2.15:httpd 指的是apac ...
- 到头来还是逃不开Java - Java13核心类
Java13核心类 没有特殊说明,我的所有学习笔记都是从廖老师那里摘抄过来的,侵删 引言 兜兜转转到了大四,学过了C,C++,C#,Java,Python,学一门丢一门,到了最后还是要把Java捡起来 ...
- 理解 nodeJS 中的 buffer,stream
在Node.js开发中,当遇到 buffer,stream,和二进制数据处理时,你是否像我一样,总是感到困惑?这种感觉是否会让你认为不了解它们,以为它们不适合你,认为而这些是Node.js作者们的事情 ...
- ASP.NET Identity系列教程-1目录
https://www.cnblogs.com/hao-1234-1234/p/8857437.html ASP.NET Identity系列教程 13 Getting Started with Id ...
- 【SQL】基础概念
1.. In order to find the rows where the value for a column is or is not NULL, you would use IS NULL ...
- html 动态生成
function func_creatediv(item, index, input) { var ip = document.createElement("div"); ip.n ...