当前位置:网站首页>leetcode 72. Edit distance edit distance (medium)
leetcode 72. Edit distance edit distance (medium)
2022-07-04 22:29:00 【InfoQ】
One 、 The main idea of the topic
- 0 <= word1.length, word2.length <= 500
- word1 and word2 It's made up of lowercase letters
Two 、 Their thinking
3、 ... and 、 How to solve the problem
3.1 Java Realization
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];
}
}
Four 、 Summary notes
- 2022/7/4 Stick to one question every day
边栏推荐
猜你喜欢
湘江鲲鹏加入昇腾万里伙伴计划,与华为续写合作新篇章
Tiktok actual combat ~ the number of comments is updated synchronously
Zhiyang innovation signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry
Xiangjiang Kunpeng joined the shengteng Wanli partnership program and continued to write a new chapter of cooperation with Huawei
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
Concurrent optimization summary
Introduction and application of bigfilter global transaction anti duplication component
30余家机构联合发起数字藏品行业倡议,未来会如何前进?
With this PDF, we finally got offers from eight major manufacturers, including Alibaba, bytek and Baidu
Play with grpc - go deep into concepts and principles
随机推荐
i. Mx6ull driver development | 24 - platform based driver model lights LED
Which securities company has the lowest Commission for opening an account online? I want to open an account. Is it safe to open an account online
湘江鲲鹏加入昇腾万里伙伴计划,与华为续写合作新篇章
LOGO special training camp section I identification logo and Logo Design Ideas
Representation of confidence interval
Cadre WebGIS - kalrry
Short video system source code, click the blank space of the screen, the keyboard does not automatically stow
# 2156. Find the substring of the given hash value - post order traversal
TLA+ 入门教程(1):形式化方法简介
Basic structure of PostgreSQL - table
常用的开源无代码测试工具
LOGO特訓營 第三節 首字母創意手法
Energy momentum: how to achieve carbon neutralization in the power industry?
sqlserver对数据进行加密、解密
VIM from dislike to dependence (23) -- the last gossip
WebGIS框架---kalrry
傳智教育|如何轉行互聯網高薪崗比特之一的軟件測試?(附軟件測試學習路線圖)
High school physics: linear motion
ACM Multimedia 2022 | 视觉语言预训练模型中社会偏见的反事实衡量和消除
2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { fmt.Pri