一、题目说明

题目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. C语言 小技巧函数方法总结

    1.使用^(异或) 不引入第三变量交换两个变量的值. /* 交换 int a 和 int b 的值*/ #include <stdio.h> int main(int argc, char ...

  2. 莫凡_linux

    1.安装软件 2.基本命令ls和cd cd 指令 第一个要知道的指令就是怎么样去到你想去的地方. cd (Change Directory) 就是干这个的. 找到 Linux 的 terminal 窗 ...

  3. 微信小程序——自定义菜单切换栏tabbar组件

    效果图: wxml代码: <view class="top_tabbar" > <block wx:for="{{itemName}}" wx ...

  4. [CF1216C] White Sheet - 离散化,模拟

    虽然分类讨论应该是比较推崇的解法,但是我就是喜欢暴力 #include <bits/stdc++.h> using namespace std; #define int long long ...

  5. Winform form窗体已弹出框的形式出现并回传值

    From2(弹出框)回传数据到From1 Form1(数据接收form): public string Sstr; private void button1_Click(object sender, ...

  6. 在 window 上卸载 mysql

    1.停止 mysql 服务 开始——>控制面板——>管理工具——>服务,停掉 MySQL 的服务 2.卸载安装包 控制面板-添加删除程序,找到MySQL,卸载(可能会有多个安装包,需 ...

  7. Android 开发 facebook分享,登陆,获取信息

    1 搭建开发环境    1.1 在Facebook官网SDK中,下载4.0.0的SDK包.        1.2 使用Eclipse导入SDK包中的Facebook工程,并添加android-supp ...

  8. python dataframe筛选列表的值转为list【常用】

    网上方法参差不齐,无注释解释不好秒懂,没有自己想要的,故自己试验一番~ 1. 筛选列表中,当b列中为’1’时,所有c的值,然后转为list 2 .筛选列表中,当a列中为'one',b列为'1'时,所有 ...

  9. Excel VBA 如何在工作表上使用Option Button按钮

    应用场景 在Excel的页面上放一个“确认”按钮,再放几个Option Button按钮,编写代码,点击“确认”按钮,判断出选择了哪个Option按钮, 然后根据选择不同的Option Button去 ...

  10. SpringBoot学习- 2、使用IDEA创建项目

    SpringBoot学习足迹 上一节使用sts创建项目,感觉只是基于eclipse做了一些界面定制,还是改使用IDEA开发,为了跟上时代,将使用IDEA最新版本,安装各种最新插件. 1.下载IDEA ...