当前位置:网站首页>动态规划-力扣
动态规划-力扣
2022-07-23 09:56:00 【情、狠现实】
动态规划
编辑距离

解析
- 题目中能过进行动态变化的有三种方式,插入、删除与修改一个字符。
删 m[i][j] = m[i - 1][j] + 1
改 m[i][j] = m[i - 1][j - 1] + 1
增 m[i][j] = m[i ][j - 1] + 1 - 初始状态:如果没有任意一个能过匹配则需要整个单词长度次数。
m[i][0] = i
m[0][j] = j
代码
class Solution {
public int minDistance(String word1, String word2) {
/* a[i - 1] -> b[j - 1] k => a[i] -> b[j] k + 1 a[i - 1] -> b[j] k => a[i] -> b[j] k + 1 a[i] -> b[j - 1] k => a[i] -> b[j] k + 1 m[i][0] = i m[0][j] = j */
char[] w1 = word1.toCharArray();
char[] w2 = word2.toCharArray();
final int l1 = w1.length;
final int l2 = w2.length;
int[][] m = new int[l1 + 1][l2 + 1];
for (int i = 0; i <= l1; i++) {
m[i][0] = i;
}
for (int i = 0; i <= l2; i++) {
m[0][i] = i;
}
for (int i = 1; i <= l1; i++) {
for (int j = 1; j <= l2; j++) {
if (w1[i - 1] == w2[j - 1]) {
m[i][j] = m[i - 1][j - 1];
} else {
m[i][j] = 1 + Math.min(m[i -1][j - 1], Math.min(m[i - 1][j], m[i][j - 1]));
}
}
}
return m[l1][l2];
}
}
边栏推荐
- Transferred from Yuxi information disclosure: products such as mRNA covid-19 vaccine and Jiuzhou horse tetanus immunoglobulin are expected to be on the market within this year.
- Kettle实现共享数据库连接及插入更新组件实例
- What is per title encoding?
- Blazor快速实现扫雷(MineSweeper)
- C thread lock and single multithreading are simple to use
- [untitled] test [untitled] test
- ESP三相SVPWM控制器的simulink仿真
- Leetcode-227-basic calculator||
- [test platform development] 21. complete sending interface request and display response header information
- 一道代码题看 CommonJS 模块化规范
猜你喜欢

Linked list review!

@Feignclient detailed tutorial (illustration)

Multiple backpacks!

C thread lock and single multithreading are simple to use
![[applet automation minium] i. framework introduction and environment construction](/img/1f/95b78e6574c3af3ff7abcf5db838f5.png)
[applet automation minium] i. framework introduction and environment construction

General of MySQL_ Log log

初识C语言函数

面试官:生成订单30分钟未支付,则自动取消,该怎么实现?

如何实现多个传感器与西门子PLC之间485无线通讯?

基于simulink的双闭环矢量控制的电压型PWM整流器仿真
随机推荐
Qt| imitation text floating letter
精品国创《少年歌行》数字藏品开售,邀你共铸少年武侠江湖梦
爬虫中selenium实现自动给csdn博主文章点收藏
利用shell脚本实现封禁扫描频率过高的ip
ESP三相SVPWM控制器的simulink仿真
基于nextcloud构建个人网盘
[test platform development] 23. interface assertion function - save interface assertion and edit echo
生成订单号
Deep learning single image 3D face reconstruction
Kettle implémente une connexion de base de données partagée et insère une instance de composant de mise à jour
Leetcode: 17. letter combination of phone number
Oracle 报表常用sql
[record of question brushing] 19. Delete the penultimate node of the linked list
The pit trodden by real people tells you to avoid the 10 mistakes often made in automated testing
什么是Promise?Promise有什么好处
mysql函数汇总之数学函数
直播课堂系统02-搭建项目环境
【无标题】
Official wechat product! Applet automation framework minium sharing Preview
[applet automation minium] III. element positioning - use of wxss selector