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. forbidden Derby database starting with weblogic instance

    Now doing a new project  which  choose the newest weblogic 12.1.2.0.0 as web container.But found the ...

  2. Python any() 函数

    Python any() 函数  Python 内置函数 描述 any() 函数用于判断给定的可迭代参数 iterable 是否全部为 False,则返回 False,如果有一个为 True,则返回 ...

  3. 关于scanf的算法(位操作)

    题目要求:输入有12行数据,每一行分别是每个月的余额.计算他们的平均值后输出.在输出时要在前面加上“$”,并在四舍五入后保留小数点后两位. 方法1: float a,b; main() { ;) b+ ...

  4. windows 安装git

    搭建环境:windo server 2012 方案: 服务器端:gitblit.下载地址:http://www.gitblit.com/ 客户端:git for windows.下载地址:https: ...

  5. Introduction Sockets to Programming in C using TCP/IP

    Introduction Computer Network: hosts, routers, communication channels Hosts run applications Routers ...

  6. WebService超时

    1.web.config配置,<system.web></system.web>里面增加:<httpRuntime maxRequestLength="1024 ...

  7. 复利计算--web版--总结--软件工程

    复利计算项目 估计用时 实际用时 时间(小时) 5.5小时  6.5小时 总共代码行 500  550 功能包含 单利/复利计算,本金计算,求投资年限,求投资项目利率估计 (计算利息和,计算时间,计算 ...

  8. sql时间戳转日期格式

    FROM_UNIXTIME(ctime, '%Y-%m-%d %H:%i:%s')

  9. nginx 负载均衡 使用ip_hash方式解决session问题 测试

    ip_hash的方式比较弱智,但是在一般情况下是挺有效的~~,如果能保证nginx是最上一层的代理,那么能够得到用户的ip是真实位置,就能做到负载,但是一家公司的所有员工其实走的是同一个ip,那么在这 ...

  10. 常见的http response

    200            //OK                                 400            //bad request 401           //Una ...