当前位置:网站首页>【LeetCode】最长公共子序列(动态规划)
【LeetCode】最长公共子序列(动态规划)
2022-08-03 23:00:00 【小七mod】
一、题目
给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度。如果不存在 公共子序列 ,返回 0 。
一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。
例如,"ace" 是 "abcde" 的子序列,但 "aec" 不是 "abcde" 的子序列。
两个字符串的 公共子序列 是这两个字符串所共同拥有的子序列。
示例 1:
输入:text1 = "abcde", text2 = "ace" 输出:3 解释:最长公共子序列是 "ace" ,它的长度为 3 。
示例 2:
输入:text1 = "abc", text2 = "abc" 输出:3 解释:最长公共子序列是 "abc" ,它的长度为 3 。
示例 3:
输入:text1 = "abc", text2 = "def" 输出:0 解释:两个字符串没有公共子序列,返回 0 。
提示:
1 <= text1.length, text2.length <= 1000
text1
和text2
仅由小写英文字符组成。
二、代码
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
if (text1 == null || text1.length() == 0 || text2 == null || text2.length() == 0) {
return 0;
}
char[] str1 = text1.toCharArray();
char[] str2 = text2.toCharArray();
int[][] dp = new int[str1.length][str2.length];
dp[0][0] = str1[0] == str2[0] ? 1 : 0;
for (int j = 1; j < str2.length; j++) {
dp[0][j] = str1[0] == str2[j] ? 1 : dp[0][j - 1];
}
for (int i = 1; i < str1.length; i++) {
dp[i][0] = str1[i] == str2[0] ? 1 : dp[i - 1][0];
}
for (int i = 1; i < str1.length; i++) {
for (int j = 1; j < str2.length; j++) {
int p1 = dp[i - 1][j];
int p2 = dp[i][j - 1];
int p3 = str1[i] == str2[j] ? dp[i - 1][j - 1] + 1 : dp[i - 1][j - 1];
dp[i][j] = Math.max(p1, Math.max(p2, p3));
}
}
return dp[str1.length - 1][str2.length - 1];
}
}
边栏推荐
- redis持久化方式
- ML之interpret:基于titanic泰坦尼克是否获救二分类预测数据集利用interpret实现EBC模型可解释性之全局解释/局部解释案例
- Scala basics [regular expressions, framework development principles]
- for loop exercises
- Interpretation of ML: A case of global interpretation/local interpretation of EBC model interpretability based on titanic titanic rescued binary prediction data set using interpret
- Code Casual Recording Notes_Dynamic Programming_416 Segmentation and Subsetting
- utlis thread pool
- Summary bug 】 【 Elipse garbled solution project code in Chinese!
- Work Subtotal QT Packing
- 什么是memoization,它有什么用?
猜你喜欢
Quickly build a website with static files
2022-08-03 Oracle executes slow SQL-Q17 comparison
Code Casual Recording Notes_Dynamic Programming_416 Segmentation and Subsetting
静态文件快速建站
override learning (parent and child)
Software testing is seriously involution, how to improve your competitiveness?
Redis persistence method
"Digital Economy Panorama White Paper" Financial Digital User Chapter released!
CAS: 178744-28-0, mPEG-DSPE, DSPE-mPEG, methoxy-polyethylene glycol-phosphatidylethanolamine supply
BMN: Boundary-Matching Network for Temporal Action Proposal Generation Reading Notes
随机推荐
【RYU】rest_router.py源码解析
静态文件快速建站
Click the icon in Canvas App to generate PDF and save it to Dataverse
目标检测技术研究现状及发展趋势
navicat 连接 mongodb 报错[13][Unauthorized] command listDatabases requires authentication
MCS-51单片机,定时1分钟,汇编程序
Golang Chapter 1: Getting Started
Network basic learning series four (network layer, data link layer and some other important protocols or technologies)
AOSP CameraLatencyHistogram的原理与使用
ML之interpret:基于titanic泰坦尼克是否获救二分类预测数据集利用interpret实现EBC模型可解释性之全局解释/局部解释案例
UVa 437 - The Tower of Babylon (White Book)
获国际权威认可 | 云扩科技入选《RPA全球市场格局报告,Q3 2022》
Unity2021 releases WebGL fog effect disappearing problem
Software testing is seriously involution, how to improve your competitiveness?
CAS:178744-28-0,mPEG-DSPE,DSPE-mPEG,甲氧基-聚乙二醇-磷脂酰乙醇胺供应
伴随着元宇宙、web3.0等概念的兴起,数字人、数字场景等诸多数字化的形态开始出现
BMN: Boundary-Matching Network for Temporal Action Proposal Generation阅读笔记
直播预告 | 构建业务智联,快速拥抱财务数字化转型
utils timer
最小化安装debian11