刷题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 ...
随机推荐
- 2020牛客寒假算法基础集训营6 E.立方数(唯一分解定理 素数筛)
https://ac.nowcoder.com/acm/contest/3007/E 放下题解 #include<bits/stdc++.h> using namespace std; t ...
- MySQL 8 通用二进制发行版安装
安装前的一些说明: 检查平台兼容性: https://www.mysql.com/support/supportedplatforms/database.html 如果是在RedHat7版本安装的话, ...
- 深入浅出Mybatis系列二-配置简介(mybatis源码篇)
注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 上篇文章<深入浅出Mybatis系列(一)---Mybatis入门>, ...
- MatchQuotesPastEndOfLine
MatchQuotesPastEndOfLine: 设定值:Yes/No 作用:当读取平面文件时,是否将双引号括起来部分整体视为单个字段值,比如以下平面文件: ID, Name, City , To ...
- StaticFileMiddleware 解析
说明:由于部分产品没有静态资源的管理,我突然想到能不能用现有的静态文件中间件的功能调整一下实现多组织件上传文件的隔离呢?那第一步先看懂 StaticFileMiddleware做了什么吧. ...
- aws申请ec2实例后如何用root用户登录
ec2默认禁用root用户登录,我们创建ec2实例后如何知道使用什么用户登录,有两种方法? 方法一:根据我们选择的镜像来判断用什么用户登录:镜像:centos 用户centos镜像:aws 用户:ec ...
- Learn from Niu
创新的源头来自于思考,尤其是深度思考: 1. 读博过程必然会经历痛苦,思考,深度思考这么一个过程,其中思考是最重要的,尤其是深度思考. 思考之后才是创新. 2. 借用其他的知识弥补这个领域的知识,不简 ...
- Appium+Python移动端(Android)自动化测试环境搭建
一.安装JDK 下载好jdk安装包后直接下一步直至安装完成即可,安装完JDK后配置环境变量 :计算机→属性→高级系统设置→高级→环境变量: 系统变量→新建 JAVA_HOME 变量 变量值填写jdk的 ...
- 海康 - 终端服务器 - TS-5012-F
简介 型号描述 主要特点 典型应用 技术参数 型号 参数 TS-5012-F (1T) TS-5012-F (2T) TS-5012-F (4T) TS-5012-F (8T) 系统参数 ...
- layui树形结构更改
/* * 将json字符串更改为layui.tree所用的数据结构类型,输出仍然为json字符串 * tanghao 7.29 */ function dataToTreeData(oData_str ...