刷题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 ...
随机推荐
- LeetCode subarray-sum-equals-k题解 前缀和+Hash表+枚举——线性做法
文章目录 题意 思路 连续子数组的和sum[i,j] 源码 结果记录 题意 给定一个数组,求连续的子数组的和为k的子数组个数. 思路 连续子数组的和sum[i,j] sum[i,j]=∑k=ijAk( ...
- SYZOJP186 你猜猜是不是DP 二分+hash解法
SYZOJP186 你猜猜是不是DP题解 题目传送门 现在给两个仅包含小写字母的字符串a,b ,求a 与b的最长公共连续子串的长度. 对于20%的数据,a,b长度 ∈ [1, 200] 对于50%的数 ...
- react-native构建基本页面2---轮播图+九宫格
配置首页的轮播图 轮播图官网 运行npm i react-native-swiper --save安装轮播图组件 导入轮播图组件import Swiper from 'react-native-swi ...
- 免费馅饼 HDU - 1176 基础dp
/*题都是有一个状态转移方程式 , 只要推出方程式就问题不大了,首 先对于gameboy来说他下一秒只能 在0~10这十一个位置移动, 而对于1~9这九个位置来说他可以移动(假设他现在的位置为x)到x ...
- CLOUD将excel数据引入单据体
http://club.kingdee.com/forum.php?mod=viewthread&tid=989239 http://club.kingdee.com/forum.php?mo ...
- C++面向对象编程实例
实例一.四位运算符操作 main.cpp. #include <iostream> #include "operator_1.h" #include <stdio ...
- 打开UML类图的正确姿势
UML(Unified Modeling Language) 统一建模语言,又称标准建模语言.是用来对软件密集系统进行可视化建模的一种语言.UML的定义包括UML语义和UML表示法两个元素.UML是在 ...
- 根据CPU内核创建多进程
from multiprocessing import Pool import psutil cpu_count = psutil.cpu_count(logical=False) #1代表单核CPU ...
- Java 散列集笔记
散列表 散列表(hash table)为每个对象计算一个整数,称为散列码(hash code). 若需要自定义类,就要负责实现这个类的hashCode方法.注意自己实现的hashCode方法应该与eq ...
- 解决pycharm创建github工程但push失败的问题
1.运行git-cmd.exe; 2.d: cd 工程路径 3.执行命令:git remote set-url origin https://github.com/wawj901124/jmet ...