当前位置:网站首页>Dynamic planning - force buckle
Dynamic planning - force buckle
2022-07-23 15:08:00 【Love and hard reality】
List of articles
Dynamic programming
Edit distance

analysis
- There are three ways to make dynamic changes in the topic , Insert 、 Delete and modify a character .
Delete m[i][j] = m[i - 1][j] + 1
Change m[i][j] = m[i - 1][j - 1] + 1
increase m[i][j] = m[i ][j - 1] + 1 - The initial state : If no one can match, the whole word length is required .
m[i][0] = i
m[0][j] = j
Code
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];
}
}
边栏推荐
- Fastapi application joins Nacos
- 一道代码题看 CommonJS 模块化规范
- Redis | 非常重要的中间件
- CBOC signal modulation and demodulation simulation based on MATLAB, output its correlation, power spectrum and frequency offset tracking
- IO流之 字节流 & 字符流
- raid homes and plunder houses!
- Shell script case ---3
- mysql 之general_log日志
- Full backpack!
- Getting started with Prometheus (III)
猜你喜欢

21 - vertical traversal of binary tree

Openharmony South learning notes - hi3861+hc-sr04 ultrasonic testing

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.

Live classroom system 01 database table design

Live classroom system 03 supplement model class and entity

LeetCode-227-基本计算器||

Educational Codeforces Round 132 (Rated for Div. 2) D. Rorororobot

Axure进阶

Building personal network disk based on nextcloud

基于simulink的双闭环矢量控制的电压型PWM整流器仿真
随机推荐
21 - vertical traversal of binary tree
General of MySQL_ Log log
直播课堂系统03补充-model类及实体
Linux: analysis of the basic use of vim editor
The best time to buy and sell stocks
Simulation de modulation et de démodulation du signal CBOC basée sur MATLAB, sortie de corrélation, spectre de puissance et suivi de décalage de fréquence
AVX指令集加速矩阵乘法
js判断元素是否到滚动到顶部
Kettle implémente une connexion de base de données partagée et insère une instance de composant de mise à jour
js拖拽元素
double类型精度丢失问题以及解决方法
真人踩过的坑,告诉你避免自动化测试常犯的10个错误
C thread lock and single multithreading are simple to use
Kettle implements shared database connection and insert update component instances
Matlab simulation of Turbo code error rate performance
Reflection invokes transaction methods, resulting in transaction invalidation
581. Shortest unordered continuous subarray
初识C语言函数
什么是Promise?Promise有什么好处
338. Bit count