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数组,其中dp[i][j]表示[i,j]区间内的字符串的最长回文子序列,那么对于递推公式我们分析一下,如果s[i]==s[j],那么i和j就可以增加2个回文串的长度,我们知道中间dp[i + 1][j - 1]的值,那么其加上2就是dp[i][j]的值。如果s[i] != s[j],那么我们可以去掉i或j其中的一个字符,然后比较两种情况下所剩的字符串谁dp值大,就赋给dp[i][j],那么递推公式如下:

/  dp[i + 1][j - 1] + 2                       if (s[i] == s[j])

dp[i][j] =

\  max(dp[i + 1][j], dp[i][j - 1])        if (s[i] != s[j])

解法一:

class Solution {
public:
int longestPalindromeSubseq(string s) {
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n));
for (int i = n - ; i >= ; --i) {
dp[i][i] = ;
for (int j = i + ; j < n; ++j) {
if (s[i] == s[j]) {
dp[i][j] = dp[i + ][j - ] + ;
} else {
dp[i][j] = max(dp[i + ][j], dp[i][j - ]);
}
}
}
return dp[][n - ];
}
};

我们可以对空间进行优化,只用一个一维的dp数组,参见代码如下:

解法二:

class Solution {
public:
int longestPalindromeSubseq(string s) {
int n = s.size(), res = ;
vector<int> dp(n, );
for (int i = n - ; i >= ; --i) {
int len = ;
for (int j = i + ; j < n; ++j) {
int t = dp[j];
if (s[i] == s[j]) {
dp[j] = len + ;
}
len = max(len, t);
}
}
for (int num : dp) res = max(res, num);
return res;
}
};

下面是递归形式的解法,memo数组这里起到了一个缓存已经计算过了的结果,这样能提高运算效率,使其不会TLE,参见代码如下:

解法三:

class Solution {
public:
int longestPalindromeSubseq(string s) {
int n = s.size();
vector<vector<int>> memo(n, vector<int>(n, -));
return helper(s, , n - , memo);
}
int helper(string& s, int i, int j, vector<vector<int>>& memo) {
if (memo[i][j] != -) return memo[i][j];
if (i > j) return ;
if (i == j) return ;
if (s[i] == s[j]) {
memo[i][j] = helper(s, i + , j - , memo) + ;
} else {
memo[i][j] = max(helper(s, i + , j, memo), helper(s, i, j - , memo));
}
return memo[i][j];
}
};

类似题目:

Palindromic Substrings

Longest Palindromic Substring

参考资料:

https://discuss.leetcode.com/topic/78603/straight-forward-java-dp-solution

https://discuss.leetcode.com/topic/78799/c-beats-100-dp-solution-o-n-2-time-o-n-space

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Longest Palindromic Subsequence 最长回文子序列的更多相关文章

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

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

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

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

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

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

  4. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  5. LeetCode:Longest Palindromic Substring 最长回文子串

    题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  6. Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

    Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...

  7. [LeetCode] 5. Longest Palindromic Substring 最长回文子串

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  8. 【LeetCode】5. Longest Palindromic Substring 最长回文子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...

  9. [leetcode]5. Longest Palindromic Substring最长回文子串

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

随机推荐

  1. Matlab绘图基础——axis设置坐标轴取值范围

    peaks; axis tight  %Set the axis limits to equal the range of the data  axis square axis 'auto x'  % ...

  2. 关于redis数据库的简单思考

    redis数据库中有以下几种数据类型: 字符串,哈希,列表,集合,有序集合 它们应用的场景如下: 字符串用法单一,用于存储一个key的值,用于一一对应的场合 列表作为数组来使用 对于哈希,特别适用于存 ...

  3. input输入框限制输入正整数、小数、字母、文字

    有的时候需要限制input的输入格式: 例如,输入大于0的正整数 <input onkeyup="if(this.value.length==1){this.value=this.va ...

  4. css3控制div上下跳动

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. 在使用Qt5.8完成程序动态语言切换时遇到的问题

    因为之前了解过一些Qt国际化的东西,所以在写程序的时候需要显示给用户的字符都使用了 tr(" ")的形式,然后使用 Qt Linguist得到相应的 qm(Qt message)文 ...

  6. 用js来实现那些数据结构(数组篇01)

    在开始正式的内容之前,不得不说说js中的数据类型和数据结构,以及一些比较容易让人混淆的概念.那么为什么要从数组说起?数组在js中是最常见的内存数据结构,数组数据结构在js中拥有很多的方法,很多初学者记 ...

  7. 基础补充:使用xlrd模块读取excel文件

    因为接口测试用例使用excel文件来维护的,所以有必要学习下操作excel的基本方法 参考博客:python 3 操作 excel 把自己练习的代码贴出来,是一些基本的操作,每行代码后面都加了注释. ...

  8. fs检测文件夹状态

    var http = require("http"); var fs = require("fs"); var server = http.createServ ...

  9. 【iOS】Swift GCD-下

    欢迎来到本GCD教程的第二同时也是最终部分! 在第一部分中,你学到了并发,线程以及GCD的工作原理.通过使用dispatch_barrrier和dispatch_sync,你做到了让PhotoMana ...

  10. Flask 视图

    写个验证用户登录的装饰器:在调用函数前,先检查session里有没有用户 from functools import wraps from flask import session, abort de ...