当前位置:网站首页>72. 编辑距离 ●●●
72. 编辑距离 ●●●
2022-06-10 19:49:00 【chenyfan_】
72. 编辑距离 ●●●
描述
给你两个单词 word1 和 word2, 请返回将 word1 转换成 word2 所使用的最少操作数 。
你可以对一个单词进行如下三种操作:
- 插入一个字符
- 删除一个字符
- 替换一个字符
示例
输入:word1 = “horse”, word2 = “ros”
输出:3
解释:
horse -> rorse (将 ‘h’ 替换为 ‘r’)
rorse -> rose (删除 ‘r’)
rose -> ros (删除 ‘e’)
题解
1. 动态规划(子序列问题)
dp[i][j]表示以 s[i-1], t[j-1]为末尾时所需操作步数;if(s[i-1] == t[j-1]) dp[i][j] = dp[i-1][j-1];
如果当前这对字符相等,等于上一组数的步数,即都不操作if(s[i-1] != t[j-1]) dp[i][j] = min(dp[i-1][j-1], min(dp[i][j-1], dp[i-1][j])) + 1;
如果字符不相等,则根据步数来判断删除当前遍历行还是列上的字符(在字符串1删除元素,等效于在另一字符串增加元素),或者在上一组的基础上替换其中一个字符。- 边界条件初始化:
dp[0][j] = j;dp[i][0] = i;意味着空字符与其他字符串匹配的步数; - 双层循环,从上到下,从左到右。
时间复杂度: O ( n × m ) O(n × m) O(n×m)
空间复杂度: O ( n × m ) O(n × m) O(n×m)
class Solution {
public:
int minDistance(string word1, string word2) {
int len1 = word1.length();
int len2 = word2.length();
vector<vector<int>> dp(len1+1, vector<int>(len2+1, 0));
for(int i = 0; i <= len1; ++i) dp[i][0] = i; // 边界条件初始化
for(int j = 0; j <= len2; ++j) dp[0][j] = j;
for(int i = 1; i <= len1; ++i){
for(int j = 1; j <= len2; ++j){
if(word1[i-1] == word2[j-1]){
dp[i][j] = dp[i-1][j-1]; // 相等时,等于上一组数的步数
}else{
dp[i][j] = min(dp[i-1][j-1], min(dp[i][j-1], dp[i-1][j])) + 1; // 根据更小的步数来判断操作
}
}
}
return dp[len1][len2];
}
};
边栏推荐
- 一个10年左右的老程序员说:简单CRUD功能进入无码开发时代1 之 增删改查接口信息
- Error code 1129, state HY000, host 'xxx' is blocked because of many connection errors
- LeetCode:1037. 有效的回旋镖————简单
- The new audio infinix machine appears in the Google product library, and Tecno CaMon 19 is pre installed with Android 13
- 六级考试-商务英语-考前最后一背
- 揭秘:春晚微信红包,是如何抗住 100 亿次请求的?
- Cut rope / integer split
- 农产品期货开户的条件是什么?现在开户的手续费是多少?
- 自定义日期组件,左右按钮控制向前或向后翻年、翻月、翻周、翻日
- C语言 浮点数 储存形式
猜你喜欢

详解三级缓存解决循环依赖

Knowledge map / relationship visualization
![js基础及常考面试题之 [] == ![]结果为true, []==[]结果为false 详解](/img/42/bcda46a9297a544b44fea31be3f686.png)
js基础及常考面试题之 [] == ![]结果为true, []==[]结果为false 详解

RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.device_count() is 1.

CVPR 2022 Tsinghua University proposed unsupervised domain generalization (UDG)

「Bug」问题分析 RuntimeError:which is output 0 of ReluBackward0

暗黑破坏神不朽数据库怎么用 暗黑破坏神手游不朽数据库使用方法

canvas 高级功能(中)

Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading

自注意力(self-attention)和多头注意力(multi-head attention)
随机推荐
牛客网:数组中出现次数超过一半的数字
游戏兼容性测试(通用方案)
安全隐患?意义有限?挡不住真煮迷你厨具火爆618
CVPR 2022丨清华大学提出:无监督域泛化 (UDG)
ResourceNotFoundException : Unable to find resource
Knowledge map / relationship visualization
Uni app custom navigation
pytorch深度学习——卷积操作以及代码示例
中国工科研究生200多篇英文论文中最常见的习惯(The Most Common Habits from more than 200 English Papers written by Gradua)
MySQL数据库基础
View play and earn will lead crypto games astray
The old programmer said: stop translating the world, developers should return to programming
暗黑破坏神不朽数据库怎么用 暗黑破坏神手游不朽数据库使用方法
C language floating point number storage form
pdf.js-----js解析pdf文件实现预览,并获取pdf文件中的内容(数组形式)
連接mysql報錯 errorCode 1129, state HY000, Host ‘xxx‘ is blocked because of many connection errors
Microsoft Word 教程「5」,如何在 Word 中更改页边距、创建新闻稿栏?
Fs4060a is a 4.2v/3a charging IC
CVPR 2022 Tsinghua University proposed unsupervised domain generalization (UDG)
Recommend a crud tool that may become the best crud tool in 2019 during the National Day