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 - ];
}
}

最新文章

  1. Android SDK Android NDK Android Studio 官方下载地址
  2. cocoaPod的使用
  3. android- 菜单
  4. 在C#中??和?分别是什么意思?
  5. MySQL高可用方案选型参考
  6. linux中hosts文件的修改
  7. 15个web前端的美轮美奂的 jQuery 图片特效
  8. jQuery技术内幕电子版4
  9. 一般处理程序中使用Session出现未将对象引用设置到对象的实例
  10. AIX安装SSH
  11. shell编程其实真的很简单(五)
  12. React学习小结(二)
  13. UOJ 241. 【UR #16】破坏发射台 [矩阵乘法]
  14. .net framework 4.5 +steeltoe+ springcloud(二) 实现服务发现与调用功能
  15. struts2框架学习之第一天
  16. Linux下所有命令失效的解决方法
  17. spring事件机制
  18. CentOS安装Emacs文本编辑器
  19. 二分查找 Binaryserach
  20. htmt 5 素材

热门文章

  1. html2canvas 使用指南
  2. vue中使用elementUi (分页器的使用)
  3. 用Python操作excel文档
  4. sql 语句中 order by 的用法
  5. iPhone 6 Plus 分辨率问题
  6. linux内核中的数据结构
  7. Git常用命令详解
  8. Rust格式化输出
  9. JS基础_变量提升和函数提升
  10. 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