当前位置:网站首页>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 坚持每天一题
边栏推荐
- MongoDB中的索引操作总结
- 湘江鲲鹏加入昇腾万里伙伴计划,与华为续写合作新篇章
- 机器学习笔记 - 互信息Mutual Information
- WebGIS框架---kalrry
- Xiangjiang Kunpeng joined the shengteng Wanli partnership program and continued to write a new chapter of cooperation with Huawei
- 服装企业为什么要谈信息化?
- 智洋创新与华为签署合作协议,共同推进昇腾AI产业持续发展
- How to remove the black dot in front of the title in word document
- Bookmark
- HDU - 2859 Phalanx(DP)
猜你喜欢

KDD2022 | 什么特征进行交互才是有效的?

Huawei Nova 10 series released Huawei application market to build a solid application security firewall

可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成

Analyzing the maker space contained in steam Education

【C语言进阶篇】数组&&指针&&数组笔试题

能源势动:电力行业的碳中和该如何实现?

广电五舟与华为签署合作协议,共同推进昇腾AI产业持续发展

QT—双缓冲绘图

并发网络模块化 读书笔记转

How was MP3 born?
随机推荐
HDU - 2859 Phalanx(DP)
Solve the problem of data disorder caused by slow asynchronous interface
HUAWEI nova 10系列发布 华为应用市场筑牢应用安全防火墙
Keep on fighting! The city chain technology digital summit was grandly held in Chongqing
File read write
文件读取写入
Representation of confidence interval
GTEST from ignorance to skillful use (1) GTEST installation
How to remove the black dot in front of the title in word document
解决异步接口慢导致的数据错乱问题
力扣3_383. 赎金信
PostgreSQL基本结构——表
1807. Replace the parentheses in the string
机器学习笔记 - 互信息Mutual Information
Super detailed tutorial, an introduction to istio Architecture Principle and practical application
How to implement Devops with automatic tools
Use blocconsumer to build responsive components and monitor status at the same time
QT - double buffer plot
From repvgg to mobileone, including mobileone code
2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)