当前位置:网站首页>2022.05.28(LC_516_最长回文子序列)
2022.05.28(LC_516_最长回文子序列)
2022-06-10 18:16:00 【Leeli9316】

方法:动态规划
①确定状态:dp[i][j]表示字符串s的下标范围[i, j]内的最长回文子序列长度;
②转移方程:if (s.charAt(i) == s.charAt(j)) {
//长度为1或2的子序列
if (j - i < 2) {
dp[i][j] = j - i + 1;
//长度大于2的子序列
} else {
dp[i][j] = dp[i + 1][j - 1] + 2;
}
} else {
dp[i][j] = Math.max(dp[i][j - 1], dp[i + 1][j]);
}
或 dp[i][i] = 1;
if (s.charAt(i) == s.charAt(j)) {
dp[i][j] = dp[i + 1][j - 1] + 2;
} else {
dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]);
}
③初始条件和边界情况:dp[i][i] = 1;当i >j时,dp[i][j] = 0;
④计算顺序:因为dp[i][j] 由 dp[i + 1][j - 1] 推出,所以外层for循环倒序,内层for循环正序。
class Solution {
public int longestPalindromeSubseq(String s) {
int n = s.length();
//dp[i][j]表示字符串s的下标范围[i, j]内的最长回文子序列长度
int[][] dp = new int[n][n];
for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
if (s.charAt(i) == s.charAt(j)) {
//长度为1或2的子序列
if (j - i < 2) {
dp[i][j] = j - i + 1;
//长度大于2的子序列
} else {
dp[i][j] = dp[i + 1][j - 1] + 2;
}
} else {
dp[i][j] = Math.max(dp[i][j - 1], dp[i + 1][j]);
}
}
}
return dp[0][n - 1];
}
}class Solution {
public int longestPalindromeSubseq(String s) {
int n = s.length();
//dp[i][j]表示字符串s的下标范围[i, j]内的最长回文子序列长度
int[][] dp = new int[n][n];
for (int i = n - 1; i >= 0; i--) {
dp[i][i] = 1;
for (int j = i + 1; j < n; j++) {
if (s.charAt(i) == s.charAt(j)) {
//当j=i+1时,dp[i][j] = dp[i + 1][i] + 2 = 0 + 2 = 2
dp[i][j] = dp[i + 1][j - 1] + 2;
//如果s.charAt(i) != s.charAt(j),
//则s[i]和s[j]不可能同时作为同一个回文子序列的首尾
} else {
dp[i][j] = Math.max(dp[i][j - 1], dp[i + 1][j]);
}
}
}
return dp[0][n - 1];
}
}
边栏推荐
- Adobe Premiere foundation - material nesting (animation of Tiktok ending avatar) (IX)
- [vulnhub range] janchow: 1.0.1
- 腾讯云数据库TDSQL-大咖论道 | 基础软件的过去、现在、未来
- Upgrade the playing method of snatching singing, integrate the climax clips of genuine music and real-time scoring ability~
- 直播预告 | 社交新纪元,共探元宇宙社交新体验
- MySQL index invalidation scenario
- Enterprise data quality management: how to evaluate data quality?
- How to play the Dragon Boat Festival "immersive cloud Tour"? That is to say, it helps "live broadcast +" new scene landing
- Detailed explanation of Lora module wireless transceiver communication technology
- nodejs-判断系统类型-获取主机名称-执行控制台命令-中文乱码
猜你喜欢

In the digital age, why should enterprises make digital transformation?

Vcsa7u3c installation tutorial

北京地铁票务系统

Live broadcast preview | a new era of social interaction, exploring new social experiences in the universe

端午“沉浸式云旅游”怎么玩?即构助力“直播+”新场景落地

In the introductory study of data visualization, we should be alert to pitfalls and misunderstandings and grasp key nodes

Metadata management, the basic construction of enterprises in the digital era

The value of Business Intelligence BI. Is visual report equal to Business Intelligence BI?

Opencv does not rely on any third-party database for face detection

Adobe Premiere基础-导入导出,合并素材,源文件编译,脱机(二)
随机推荐
基于谱加权的波束方向图分析
How to play the Dragon Boat Festival "immersive cloud Tour"? That is to say, it helps "live broadcast +" new scene landing
Three ways generated by stream lambda
In the digital age, why should enterprises make digital transformation?
Openssl1.1.1 compilation error can't locate win32/console pm in @INC
Array type of DB2 SQL pl
单纯形法代码求解(含超详细代码注释和整个流程图)
Uncertainty reasoning: let the model know that it doesn't know
Adobe Premiere基础-工具使用(选择工具,剃刀工具,等常用工具)(三)
Adobe Premiere Foundation (track related) (V)
JS Touch
Adobe Premiere Foundation (animation production - Flexible animation) (VIII)
Screen output of DB2 stored procedure, output parameters, and return result set
Pits encountered during the use of ETL (ETL Chinese garbled)
Db2 SQL PL的动态SQL
nodejs-基本架构分析-解析引擎目录-插件安装-核心模块
5. golang generics and reflection
Adobe Premiere基础-导入导出,合并素材,源文件编译,脱机(二)
Openssl1.1.1 VS2013-编译教程
VMware horizon 82111 deployment series (XVI) blast bandwidth test