当前位置:网站首页>leetcode 72. Edit Distance 编辑距离(中等)
leetcode 72. Edit Distance 编辑距离(中等)
2022-07-04 21:43:00 【InfoQ】
一、题目大意
- 0 <= word1.length, word2.length <= 500
- word1 和 word2 由小写英文字母组成
二、解题思路
三、解题方法
3.1 Java实现
public class Solution {
public int minDistance(String word1, String word2) {
int m = word1.length();
int n = word2.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0) {
dp[i][j] = j;
} else if (j == 0) {
dp[i][j] = i;
} else {
dp[i][j] = Math.min(
dp[i - 1][j - 1] + (word1.charAt(i - 1) == word2.charAt(j - 1) ? 0 : 1),
Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1)
);
}
}
}
return dp[m][n];
}
}
四、总结小记
- 2022/7/4 坚持每天一题
边栏推荐
- 开户哪家券商比较好?网上开户安全吗
- TLA+ 入门教程(1):形式化方法简介
- 做BI开发,为什么一定要熟悉行业和企业业务?
- QT—双缓冲绘图
- MongoDB聚合操作总结
- Caduceus从未停止创新,去中心化边缘渲染技术让元宇宙不再遥远
- Zhiyang innovation signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry
- Bizchart+slider to realize grouping histogram
- What is the stock account opening process? Is it safe to use flush mobile stock trading software?
- How was MP3 born?
猜你喜欢
How to implement Devops with automatic tools
Bookmark
什么是商业智能(BI),就看这篇文章足够了
从RepVgg到MobileOne,含mobileone的代码
Radio and television Wuzhou signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry
历史最全混合专家(MOE)模型相关精选论文、系统、应用整理分享
A large number of virtual anchors in station B were collectively forced to refund: revenue evaporated, but they still owe station B; Jobs was posthumously awarded the U.S. presidential medal of freedo
广电五舟与华为签署合作协议,共同推进昇腾AI产业持续发展
NAACL-22 | 在基于Prompt的文本生成任务上引入迁移学习的设置
TCP协议三次握手过程
随机推荐
保证接口数据安全的10种方案
gtest从一无所知到熟练使用(3)什么是test suite和test case
Operation of adding material schedule in SolidWorks drawing
Use blocconsumer to build responsive components and monitor status at the same time
Huawei Nova 10 series released Huawei application market to build a solid application security firewall
股票开户流程是什么?使用同花顺手机炒股软件安全吗?
广电五舟与华为签署合作协议,共同推进昇腾AI产业持续发展
Flink1.13 SQL basic syntax (I) DDL, DML
1807. Replace the parentheses in the string
KDD2022 | 什么特征进行交互才是有效的?
Nat. Commun.| 机器学习对可突变的治疗性抗体的亲和力和特异性进行共同优化
Why do you have to be familiar with industry and enterprise business when doing Bi development?
283. 移动零-c与语言辅助数组法
[optimtool.unconstrained] unconstrained optimization toolbox
Solana链上应用Crema因黑客攻击停运
Cloudcompare & open3d DBSCAN clustering (non plug-in)
close系统调用分析-性能优化
机器人相关课程考核材料归档实施细则2022版本
GTEST from ignorance to skillful use (1) GTEST installation
Deveco device tool 3.0 release brings five capability upgrades to make intelligent device development more efficient