当前位置:网站首页>Brute force recursion to dynamic programming 07 (516. Longest palindrome subsequence)
Brute force recursion to dynamic programming 07 (516. Longest palindrome subsequence)
2022-08-03 02:33:00 【Taotao can't learn English】
516.最长回文子序列
给定一个字符串 s ,找到其中最长的回文子序列,并返回该序列的长度.可以假设 s 的最大长度为 1000 .
示例 1:
输入: “bbbab”
输出: 4
一个可能的最长回文子序列为 “bbbb”.
示例 2:
输入:“cbbd”
输出: 2
一个可能的最长回文子序列为 “bb”.
提示:
- 1 <= s.length <= 1000
- s 只包含小写英文字母
暴力递归
Start comparing left and right,If left and right are the same position,That must be a palindrome sequence,返回1,If left and right are adjacent,如果左右相等,则返回2 ,Because both are members of a palindrome subsequence;否则返回1,两个只有1can be a member of a palindromic subsequence.
其他情况:不以 L 开头,不以 R 结尾
以 L 开头,不以 R 结尾
不以 L 开头,以 R 结尾
以 L 开头,以 R 结尾 (This is to compare whether this situation exists or not,There are immediate consequences+2,Both are members of the result)
Take the largest of the four
public int longestPalindromeSubseq(String s) {
return process(s.toCharArray(), 0, s.length() - 1);
}
public int process(char[] s, int L, int R) {
if (L == R) {
return 1;
} else if (L + 1 == R) {
//如果只有两位 0 和 1 位
return s[L] == s[R] ? 2 : 1;
} else {
//其他情况
//以不以L开头 不以R结尾
int NLNR = process(s, L + 1, R - 1);
//以L开头,不以R结尾
int LNR = process(s, L, R - 1);
//不以L开头,以R结尾
int NLR = process(s, L + 1, R);
//以L开头,以R结尾
int LR = s[L] == s[R] ? 2 + process(s, L + 1, R - 1) : 0;
return Math.max(Math.max(NLNR, LNR), Math.max(NLR, LR));
}
}
动态规划
First deal with the diagonal and the lines above the diagonal
Diagonally self and self are definitely palindromic,值为1
The latter if equal to the former,Then both are members of the palindrome,返回2,Otherwise one of them is a member of the palindrome,返回1.
从下往上,从左往右计算,此时 j 至少是 i + 2 了.
public int longestPalindromeSubseq2(String s) {
int n = s.length();
char[] chars = s.toCharArray();
int[][] dp = new int[n][n];
dp[n - 1][n - 1] = 1;
for (int i = 0; i < n - 1; i++) {
//When an element is on the main diagonal,Must be equal to the current one,即为1
dp[i][i] = 1;
//A line on the main diagonal
dp[i][i + 1] = chars[i] == chars[i + 1] ? 2 : 1;
}
for (int i = n - 3; i >= 0; i--) {
for (int j = i + 2; j < n; j++) {
//其他情况
//以不以L开头 不以R结尾
int NLNR = dp[i + 1][j - 1];
//以L开头,不以R结尾
int LNR = dp[i][j - 1];
//不以L开头,以R结尾
int NLR = dp[i + 1][j];
//以L开头,以R结尾
int LR = chars[i] == chars[j] ? 2 + dp[i + 1][j - 1] : 0;
dp[i][j] = Math.max(Math.max(NLNR, LNR), Math.max(NLR, LR));
}
}
return dp[0][n - 1];
}

边栏推荐
- Greenplum数据库故障分析——can not listen port
- flutter 时间戳转日期
- npm运行项目dependencies were not found: core-js/modules/es6.array.fill
- 9-WebUtil工具类.md
- DB2数据库-获取表结构异常:[jcc][t4][1065][12306][4.26.14]CharConvertionException ERRORCODE=-4220,SQLSTATE=null
- php一维数组合并
- flutter空安全问题,平时用到的数据一定要注意
- 文树勋率长沙市人大常委会主任会议成员莅临麒麟信安调研数字经济发展情况
- 236. 二叉树的最近公共祖先
- 心电记录电路设计(框图/波形以及信号放大器的选择)
猜你喜欢
随机推荐
暴力递归到动态规划 07(516. 最长回文子序列)
49. 字母异位词分组-排序法
吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(上)
random.nextint()详解
【多线程】Thread类的基本用法
【问题征集】向 iPod 之父、iPhone 联合设计者、Google Nest 创始人 Tony Fadell 提问啦
7-Redis工具类
从 npm 切换到 pnpm,真香!
Latex-查看预收录在arXiv.org上论文的TeX源文件
买了一瓶饮料
做快乐的事情
浅谈I2C知识
Wireshark数据抓包分析之传输层协议(TCP协议)
如何修复 SAP UI5 aggregation with cardinality 0..1 相关的错误消息
嵌入式开发:嵌入式基础——’ ’和” ”的区别
微信小程序--》条件与列表渲染以及WXSS模板样式
可编程逻辑控制器(PLC) : 基础、类型和应用
接口流量突增,如何做好性能优化?
SAP ABAP OData 服务如何支持修改(Update)操作试读版
PAT甲级 1051 Pop Sequence









