当前位置:网站首页>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];
}
};
边栏推荐
- Knowledge map / relationship visualization
- 35岁被裁员,还能拥有美妙人生吗?
- The excess part of the table setting is hidden. Move the mouse to display all
- 终于有人说清楚了Cookie、Session、Token的区别。详解,面试题。
- Handwritten code bind
- 堆叠条形图鼠标移入tooltip中提示过滤为0元素,实现自定义气泡
- Game compatibility test (general scheme)
- 堆排序与加强堆代码 用于记忆
- 电子招标采购商城系统:优化传统采购业务,提速企业数字化升级
- AttributeError: module ‘collections‘ has no attribute ‘MutableMapping‘
猜你喜欢

在阿里云国际上使用 OSS 和 CDN 部署静态网站

C语言 浮点数 储存形式
![[enter textbook latex record]](/img/f0/5ca60f0894d4ae544e7399d18a3a42.png)
[enter textbook latex record]

Jiangbolong forestee xp2000 PCIe 4.0 SSD multi encryption function, locking data security

CVPR 2022丨清华大学提出:无监督域泛化 (UDG)

Service management and communication, basic principle analysis

Elastic-Job的快速入门,三分钟带你体验分布式定时任务

知识图谱/关系可视化

Connexion MySQL errorcode 1129, State hy000, Host 'xxx' is Blocked because of many Connection Errors

电子招标采购商城系统:优化传统采购业务,提速企业数字化升级
随机推荐
农产品期货开户的条件是什么?现在开户的手续费是多少?
C language floating point number storage form
Knowledge map / relationship visualization
[observation] shengteng Zhixing: scene driven, innovation first, press the "acceleration key" for Intelligent Transportation
PDU session flow
中国工科研究生200多篇英文论文中最常见的习惯(The Most Common Habits from more than 200 English Papers written by Gradua)
获取列表中最大最小值的前n个数值的位置索引的四种方法
玩艺术也得学数学?
使用环绕通知对目标方法进行增强—摘抄笔记
In depth learning experience and tools
暗黑破坏神不朽数据库怎么用 暗黑破坏神手游不朽数据库使用方法
【生成对抗网络学习 其一】经典GAN与其存在的问题和相关改进
Kcon 2022 topic public selection is hot! Don't miss the topic of "favorite"
4.35v lithium battery charging IC
Tutoriel Microsoft Word "5", comment changer les marges de page et créer une barre de nouvelles en word?
Fs4100 lithium battery charging management IC input 12V to 8.4v charging IC
暗黑破坏神不朽WIKI地址 暗黑破坏神不朽数据库地址分享
mysql基础篇
江波龙 FORESEE XP2000 PCIe 4.0 SSD 多重加密功能,锁定数据安全
魔塔类游戏实现源码及关卡生成