当前位置:网站首页>【代码随想录-动态规划】T583、两个字符串的删除操作
【代码随想录-动态规划】T583、两个字符串的删除操作
2022-06-26 16:56:00 【不写博客就不爽】
T583、两个字符串的删除操作
给定两个单词 word1 和 word2 ,返回使得 word1 和 word2 相同所需的最小步数。
每步 可以删除任意一个字符串中的一个字符
题解思路:
step1、dp[i][j] 表示 word1[0:i-1] 和 word2[0:j-1] 想要达到相等,所需要删除元素的最少次数。
step2、递推公式
当 word1.charAt(i-1) == word2.charAt(j-1) 时候,则dp[i][j] = dp[i-1][j-1]
https://www.programmercarl.com/0583.%E4%B8%A4%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%88%A0%E9%99%A4%E6%93%8D%E4%BD%9C.html
第二种情况就要复杂一些,我写的时候只考虑到 dp[i][j-1]+1 这种情况,相当于只删除了 word2,所以最后提交 159/1396
public int minDistance(String word1, String word2) {
int n = word1.length();
int m = word2.length();
// 如何表示删除这个逻辑
// 上一道题目 dp[i][j]表示word1[:i-1]
// word2[:j-1]的最小步数
int[][] dp = new int[n+1][m+1];
for(int j = 0; j <= m; j++){
dp[0][j] = j;
}
for(int i = 0; i <= n; i++){
dp[i][0] = i;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(word1.charAt(i-1) == word2.charAt(j-1)){
dp[i][j] = dp[i-1][j-1];
}else{
dp[i][j] = Math.min(dp[i-1][j-1]+2, Math.min(dp[i][j-1] + 1, dp[i-1][j]+1));
}
}
}
return dp[n][m];
}
边栏推荐
- 5g is not flat and 6G is restarted. China leads wireless communication. What is the biggest advantage of 6G?
- [Error] ld returned 1 exit status
- 建立自己的网站(16)
- Sandboxed container: container or virtual machine
- Introduction to minimal API
- ACL 2022 | zero sample multilingual extracted text summarization based on neural label search
- Constructors and Destructors
- Programmer interview guide - self introduction
- She said she was tired! So tired! I want to change my career
- Over the weekend: 20000 words! Summary of JVM core knowledge, 18 serial cannons as a gift
猜你喜欢

【推荐系统学习】推荐系统架构

Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)

Viewing the task arrangement ability of monorepo tool from turborepo

Implementation of MySQL master-slave architecture
![[C language] static modifies local variables](/img/bf/9084d2e924c3e1e244568562a83d74.jpg)
[C language] static modifies local variables

数字藏品与NFT到底有何区别

Which low code platform is more friendly to Xiaobai? Here comes the professional evaluation!

Distributed Architecture Overview

SQL injection for Web Security (3)

vue--vuerouter缓存路由组件
随机推荐
【MATLAB项目实战】基于卷积神经网络与双向长短时(CNN-LSTM)融合的锂离子电池剩余使用寿命预测
Toupper function
Necessary decorator mode for 3 years' work
Count the number of each vowel letter in the string
ACL 2022 | 基于神经标签搜索的零样本多语言抽取式文本摘要
Treasure and niche CTA animation material website sharing
Viewing the task arrangement ability of monorepo tool from turborepo
链游系统开发技术方案设计丨NFT链游系统开发流程及源码
Day10 daily 3 questions (2): count the number of the largest groups
The student record consists of student number and academic performance. The data of n students have been stored in the a structure array to find out the student record with the lowest performance
Convert the decimal positive integer m into the number in the forward K (2 < =k < =9) system and output it in bits
Redis' 43 serial cannons, try how many you can carry
并发之Synchronized说明
Inspirational. In one year, from Xiaobai to entering the core Department of Alibaba, his counter attack
Fgetc() reads content from file
Alibaba's "high concurrency" tutorial "basic + actual combat + source code + interview + Architecture" is a god class
Viteconfigure project path alias
Detailed contract quantification system development scheme and technical description of quantitative contract system development
Which low code platform is more friendly to Xiaobai? Here comes the professional evaluation!
Microservice architecture practice: business management background and SSO design: SSO design