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".

Approach #1: DP. [Java]

class Solution {
public int longestPalindromeSubseq(String s) {
int len = s.length();
int[][] dp = new int[len+1][len+1]; for (int l = 1; l <= len; ++l) {
for (int i = 0; i <= len - l; ++i) {
int j = i + l - 1;
if (i == j) {
dp[i][j] = 1;
continue;
} else if (s.charAt(i) == s.charAt(j))
dp[i][j] = dp[i+1][j-1] + 2;
else
dp[i][j] = Math.max(dp[i+1][j], dp[i][j-1]); }
} return dp[0][len-1];
}
}

  

Analysis:

This problem is similar with 486. Predict the Winner.

dp[i][j] : the longest palindromic subsequence from i to j.

stage: length of substring.

for len = 1 to n:

  for i = 0 to n-len:

    j = i + len - 1;

    if s[i] == s[j]:

      dp[i][j] = dp[i+1][j-1] + 2;

    else:

      dp[i][j] = max(dp[i+1][j], dp[i][j-1]);

ans : dp[0][len-1].

Approach #2: optimization. [C++]

class Solution {
public:
int longestPalindromeSubseq(string s) {
int len = s.length();
vector<int> dp0(len, 0);
vector<int> dp1(len, 0);
vector<int> dp2(len, 0); for (int l = 1; l <= len; ++l) {
for (int i = 0; i <= len - l; ++i) {
int j = i + l - 1;
if (i == j) {
dp0[i] = 1;
continue;
} else if (s[i] == s[j]) {
dp0[i] = dp2[i+1] + 2;
} else {
dp0[i] = max(dp1[i+1], dp1[i]);
}
}
dp0.swap(dp1);
dp2.swap(dp0);
}
return dp1[0];
}
};

  

Reference:

http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-516-longest-palindromic-subsequence/

516. Longest Palindromic Subsequence的更多相关文章

  1. LN : leetcode 516 Longest Palindromic Subsequence

    lc 516 Longest Palindromic Subsequence 516 Longest Palindromic Subsequence Given a string s, find th ...

  2. 516. Longest Palindromic Subsequence最长的不连续回文串的长度

    [抄题]: Given a string s, find the longest palindromic subsequence's length in s. You may assume that ...

  3. [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  4. LC 516. Longest Palindromic Subsequence

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  5. [leetcode]516. Longest Palindromic Subsequence最大回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  6. 【LeetCode】516. Longest Palindromic Subsequence 最长回文子序列

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...

  7. 516 Longest Palindromic Subsequence 最长回文子序列

    给定一个字符串s,找到其中最长的回文子序列.可以假设s的最大长度为1000. 详见:https://leetcode.com/problems/longest-palindromic-subseque ...

  8. 【leetcode】516. Longest Palindromic Subsequence

    题目如下: 解题思路:很经典的动态规划题目,但是用python会超时,只好用C++了. 代码如下: class Solution { public: int longestPalindromeSubs ...

  9. [LeetCode] Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

随机推荐

  1. VLC接收网络串流缓冲时间的计算 (转)

    原帖地址:http://blog.csdn.net/coroutines/article/details/7472743 VLC版本2.0.1 最近研究IP-STB音视频同步问题,发现方案自带的自动S ...

  2. 从LinqPad入门Linq

    为什么要学习Linq? 在我们日常开发中,经常需要从数据库中执行各式各样的查询来获取需要的数据.但是如果需要对查询获得的数据进行二次筛选呢?linq就提供了对于可枚举类型(实现IEnumerable接 ...

  3. FORALL用法小结

    本文主要翻译.整理了ORACLE官方文档上有关FORALL的部份内容,不妥之处,还希望多和大家交流. 在发送语句到SQL引擎前,FORALL语句告知PL/SQL 引擎批挷定输入集合.尽管FORALL语 ...

  4. 更改Eclipse里的Classpath Variables M2_REPO

    M2_REPO这个classpath  variable 是不能改变的. 为什么 Eclipse 里的 Classpath Variables M2_REPO 无法修改(non modifiable) ...

  5. 模板引擎 引自 《PHP核心技术与最佳实践》

    随着web的发展,仅一门语言或者一种技术已经不能满足需求,分层架构显得越来越重要.在大型架构中,从来不会简单地应用php从头到尾实现一个完整的mvc架构.可能底层是c/java的支撑,负责密集运算和y ...

  6. LeetCode之二叉树作题java

    100. Same Tree Total Accepted: 127501 Total Submissions: 294584 Difficulty: Easy Given two binary tr ...

  7. 清华镜像站安装docker

    https://mirrors.tuna.tsinghua.edu.cn/help/docker-ce/

  8. oracle返回最大值一条记录

    虽然网上例子很多,但是试过多种办法都无效,原代码出处: https://blog.csdn.net/qyshibb/article/details/73332075 这个例子简单有效 select p ...

  9. 23-吝啬的国度(vector+深搜)

    吝啬的国度 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市, ...

  10. android studio升级方法

    android studio 更新问题: 如果被墙则采用以下步骤: 一:看版本 help-->about    AI***************** 二:查看android studio最新版 ...