一、题目说明

题目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的更多相关文章

  1. LintCode刷题笔记-- Edit distance

    标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...

  2. 【Leetcode】72 Edit Distance

    72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...

  3. 72. Edit Distance

    题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...

  4. [LeetCode] 72. Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to  ...

  5. leetCode 72.Edit Distance (编辑距离) 解题思路和方法

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  6. [LeetCode] 72. Edit Distance(最短编辑距离)

    传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...

  7. LeetCode - 72. Edit Distance

    最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...

  8. 72. Edit Distance(困难,确实挺难的,但很经典,双序列DP问题)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  9. [leetcode]72. Edit Distance 最少编辑步数

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...

随机推荐

  1. 1级搭建类102-Oracle 11g 单实例 FS(11.2.0.4+RHEL 7)公开

    项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列目前不对外发布,仅作为博客记录.如学员在实际工作过程中需提前 ...

  2. RSA学习档案

    RSA 学习档案 基本原理 随机选择两个质数p,q模数n=p*qφ(n)=(p−1)(q−1)选择加密指数e: 1 < e < φ(n)计算机密指数d: e*d % φ(n) = 1c = ...

  3. JS编解码与Java编解码的对应关系

    最近前段在导出数据时会遇到“illegal character”的异常错误,结果发现是在请求地址中请求参数包含了空白字符(其编码为%C2%A0)或者是空格字符(其编码为%20),之前对空格字符情况是做 ...

  4. centos因为安装花生壳而无法登录系统的问题

    服务器安装 phddns 花生壳 启动失败,一直卡在启动进度条页面. 解决办法 1.按F5查看卡在什么位置, 2.查看解决方法:程序卡住的情况下,直接备份资料后,卸载程序重启就可以了. 3.进入到si ...

  5. ABC155 D pair 边界处理取整

    ABC155 D pair 取整坑点 思路 很常见的一道题,二分找答案,然后看这个答案排rank?,排rank?用二分继续找一遍二分套二分即可,就是边界比较烦,老年人写的心情烦躁 老年人被取整坑的几天 ...

  6. Wannafly Camp 2020 Day 1F 乘法 - 字符串

    一开始想根据单调性双指针 后来血了才想起来负负得正 于是暴力二分答案即可 #include <bits/stdc++.h> using namespace std; #define int ...

  7. winform DataGrid排序、去掉第一的空白列

    排序: dataGridView1.Sort(dataGridView1.Columns[3], ListSortDirection.Descending); 去掉空白列: dataGridView1 ...

  8. [MongoDB] 使用PHP在MongoDB中搜索的实现

    条件操作符用于比较两个表达式并从mongoDB集合中获取数据.MongoDB中条件操作符有:(>) 大于 - $gt(<) 小于 - $lt(>=) 大于等于 - $gte(< ...

  9. python之路set

    一.set和其他集合的区别: list :允许重复的集合,修改 tuple:允许重复的集合,不修改 dict:字典 set:不允许重复的集合,set不允许重复的,列表是无序的 1.创建一个set s= ...

  10. kmp算法散记

    1. https://blog.csdn.net/abcjennifer/article/details/5794547 #include<bits/stdc++.h> using nam ...