当前位置:网站首页>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];
}
}
边栏推荐
- Three ways generated by stream lambda
- Adobe Premiere基础-导入导出,合并素材,源文件编译,脱机(二)
- Adobe Premiere基础特效(卡点和转场)(四)
- Introduction to DB2 SQL pl
- & and||
- Chapter IV data type (III)
- Openssl1.1.1 vs2013 compilation tutorial
- Beam pattern analysis based on spectral weighting
- Adobe Premiere基础(轨道相关)(五)
- How can bi help enterprises reduce labor, time and management costs?
猜你喜欢

mysql(17-触发器)

The question of enterprise managers, where have we spent our money after so many years of informatization?

直播预告 | 解构OLAP!新型多维分析架构范式全公开!Apache Doris 将带来五个重磅议题!

Adobe Premiere基础-素材嵌套(制作抖音结尾头像动画)(九)

Use of uiautomator2 automated test tool

mysql8.0(新特性小结)

Stream生成的3张方式-Lambda

数据库防火墙闪亮登场(好文共赏)

Adobe Premiere Foundation (the last step of video subtitle adding) (6)

跨域报错:When allowCredentials is true, allowedOrigins cannot contain the special value “*“
随机推荐
Seata installing the window environment
VMware horizon 82111 deployment series (XVI) blast bandwidth test
Adobe Premiere基础(视频的最后一步字幕添加)(六)
Screen output of DB2 stored procedure, output parameters, and return result set
Rewrite clear Bayesian formula with base ratio
Chapter 161 SQL function year
How to correctly understand the real-time nature of Bi?
锐捷x32pro刷openwrt开启无线160MHz
Chapter III data type (II)
In the digital age, why should enterprises make digital transformation?
Live broadcast preview | a new era of social interaction, exploring new social experiences in the universe
Salesmartly | add a new channel slack to help you close the customer relationship
最长上升子序列(LIS)洛谷
How to set up salesmartly for Google Analytics tracking
Nodejs judge system type get host name execute console command Chinese garbled code
Classic 6 pain points of data governance? This book teaches you to solve
Design and implementation of online ordering system based on SSM Rar (project source code)
端午“沉浸式云旅游”怎么玩?即构助力“直播+”新场景落地
WordPress 6.0 "Arturo Arturo" release
【Vulnhub靶场】JANGOW: 1.0.1