当前位置:网站首页>【LeetCode】最长回文子序列(动态规划)
【LeetCode】最长回文子序列(动态规划)
2022-08-03 23:00:00 【小七mod】
一、题目
给你一个字符串 s ,找出其中最长的回文子序列,并返回该序列的长度。
子序列定义为:不改变剩余字符顺序的情况下,删除某些字符或者不删除任何字符形成的一个序列。
示例 1:
输入:s = "bbbab" 输出:4 解释:一个可能的最长回文子序列为 "bbbb" 。
示例 2:
输入:s = "cbbd" 输出:2 解释:一个可能的最长回文子序列为 "bb" 。
提示:
1 <= s.length <= 1000
s
仅由小写英文字母组成
二、代码
class Solution {
public int longestPalindromeSubseq(String s) {
// 判空
if (s == null || s.length() == 0) {
return 0;
}
// 将字符串转换为字符数组
char[] str = s.toCharArray();
// 创建dp数组
int n = str.length;
int[][] dp = new int [n][n];
// 赋初值,对两个对角线赋初值
dp[n - 1][n - 1] = 1;
for (int i = 0; i < n - 1; i++) {
dp[i][i] = 1;
dp[i][i + 1] = str[i] == str[i + 1] ? 2 : 1;
}
// 按照动态转移方程对dp数组进行复制,沿对角线方向进行复制
for (int i = 2; i < n; i++) {
// 之所以这里用j表示列,是因为列的范围更好控制,因为所有的对角线最后一个节点一定都是最后一列,是固定不变的
for (int j = i; j < n; j++) {
int r = j;
int l = r - i;
// 动态转移方程
dp[l][r] = Math.max(dp[l + 1][r], dp[l][r - 1]);
if (str[l] == str[r]) {
dp[l][r] = Math.max(dp[l][r], dp[l + 1][r - 1] + 2);
}
}
}
// 返回结果
return dp[0][n - 1];
}
}
三、解题思路
利用动态规划的思路进行解题。利用已经得出的初值来逐渐退出全部可能的结果。
边栏推荐
猜你喜欢
LabVIEW code generation error 61056
Quickly build a website with static files
2022-08-03 Oracle executes slow SQL-Q17 comparison
The salary of soft testers at each stage, come to Kangkang, how much can you get?
Binary search tree to solve the fallen leaves problem
Deep integration of OPC UA and IEC61499 (1)
Creo 9.0二维草图的诊断:着色封闭环
Creo 9.0二维草图的诊断:重叠几何
静态文件快速建站
Walk the Maze BFS
随机推荐
node连接mysql数据库报错:Client does not support authentication protocol requested by server
Pytest学习-setup/teardown
encapsulation, package, access modifier, static variable
Deep integration of OPC UA and IEC61499 (1)
What is Adobe?
Another MySQL masterpiece published by Glacier (send the book at the end of the article)!!
rosbridge-WSL2 && carla-win11
What is memoization and what is it good for?
start with connect by implements recursive query
golang写的存储引擎,基于b+树,mmap
Storage engine written by golang, based on b+ tree, mmap
用队列模拟实现栈
Gains double award | know micro easily won the "2021 China digital twin solution suppliers in excellence" "made in China's smart excellent recommended products" double award!
Summary bug 】 【 Elipse garbled solution project code in Chinese!
物联网新零售模式,引领购物新潮流
Embedded systems: overview
Scala basics [regular expressions, framework development principles]
Creo 9.0二维草图的诊断:加亮开放端点
The salary of soft testers at each stage, come to Kangkang, how much can you get?
目标检测技术研究现状及发展趋势