当前位置:网站首页>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];
}
边栏推荐
- JSP第一篇 -----JSP九大内置对象(隐式对象)和四大域对象
- 全栈----跨域
- 【深度学习】基于tensorflow的小型物体识别训练(数据集:CIFAR-10)
- 8 个常用的 Wireshark 使用技巧,一看就会
- Nacos配置中心之事件订阅
- 一套开源的可快速搭建自己的物联网/智能家居系统源码
- Servlet——请求(request)与响应(response)
- torchvision.datasets.ImageFolder使用详解
- 8-jwt工具类
- Auto.js special positioning control method cannot perform blocking operations on the ui thread, please use setTimeout instead
猜你喜欢
在表格数据上,为什么基于树的模型仍然优于深度学习?
Greenplum数据库故障分析——can not listen port
PAT甲级 1051 Pop Sequence
并发模型和I/O模型介绍
vue3的keepAlive缓存组件
麒麟信安邀您抢先看 | openEuler 志高远,开源汇智创未来-开放原子全球开源峰会欧拉分论坛最详细议程出炉
Auto.js 特殊定位控件方法 不能在ui线程执行阻塞操作,请使用setTimeout代替
阿里云增强版实人认证--银行卡要素核验
2022-08-02:小红拿到了一个大立方体,该大立方体由1*1*1的小方块拼成,初始每个小方块都是白色。 小红可以每次选择一个小方块染成红色, 每次小红可能选择同一个小方块重复染色, 每次染色以后,
接口流量突增,如何做好性能优化?
随机推荐
2149. 按符号重排数组
买了一瓶饮料
关于地图GIS开发事项的一次实践整理(上)
Jenkins汉化设置
10-security登录
如何正确地配置入口文件?
麒麟信安邀您抢先看 | openEuler 志高远,开源汇智创未来-开放原子全球开源峰会欧拉分论坛最详细议程出炉
担心的事情
matlab常微分方程在传染病建模中的应用
Visual Studio中vim模拟器
【遥控器开发基础教程5】疯壳·开源编队无人机-SPI(2.4G 双机通信)
德邦科技通过注册:年营收5.8亿 国家集成电路基金为大股东
浅谈敏捷开发
【Autosar RTM】
暴力递归到动态规划 07(516. 最长回文子序列)
flutter 时间戳转日期
阿里云增强版实人认证--银行卡要素核验
升级版的冒泡排序:鸡尾酒排序(快乐小时排序)
投资的思考
torchvision.datasets.ImageFolder使用详解