Longest Palindromic Subsequence
2024-09-03 02:59:11
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000.
Example 1:
Input:
"bbbab"
Output:
4
One possible longest palindromic subsequence is "bbbb".
Example 2:
Input:
"cbbd"
Output:
2
One possible longest palindromic subsequence is "bb".
这是一道典型的dp题,用dp[][]来存储i,j两个pointer指向的string的一个char, 分别按照相等和不相等来处理即可。
class Solution {
public int longestPalindromeSubseq(String s) {
int length = s.length();
int[][] dp = new int[length][length];
for (int i = ; i < length; i++) {
dp[i][i] = ;
}
for (int subLength = ; subLength <= length; subLength++) {
for (int i = ; i + subLength <= length; i++) {
int j = subLength + i - ;
if (s.charAt(i) == s.charAt(j)) {
dp[i][j] = dp[i + ][j - ] + ;
} else {
dp[i][j] = Math.max(dp[i + ][j], dp[i][j - ]);
}
}
}
return dp[][length - ];
}
}
最新文章
- Android SDK Android NDK Android Studio 官方下载地址
- cocoaPod的使用
- android- 菜单
- 在C#中??和?分别是什么意思?
- MySQL高可用方案选型参考
- linux中hosts文件的修改
- 15个web前端的美轮美奂的 jQuery 图片特效
- jQuery技术内幕电子版4
- 一般处理程序中使用Session出现未将对象引用设置到对象的实例
- AIX安装SSH
- shell编程其实真的很简单(五)
- React学习小结(二)
- UOJ 241. 【UR #16】破坏发射台 [矩阵乘法]
- .net framework 4.5 +steeltoe+ springcloud(二) 实现服务发现与调用功能
- struts2框架学习之第一天
- Linux下所有命令失效的解决方法
- spring事件机制
- CentOS安装Emacs文本编辑器
- 二分查找 Binaryserach
- htmt 5 素材
热门文章
- html2canvas 使用指南
- vue中使用elementUi (分页器的使用)
- 用Python操作excel文档
- sql 语句中 order by 的用法
- iPhone 6 Plus 分辨率问题
- linux内核中的数据结构
- Git常用命令详解
- Rust格式化输出
- JS基础_变量提升和函数提升
- This sample is for changing from “float64” to “int” for values did unmarshal using map[string]interface{}. When it did unmarshal using map[string]interface{}, a number with “int” was changed to “floa