给定一个字符串s,找到其中最长的回文子序列。可以假设s的最大长度为1000。

详见:https://leetcode.com/problems/longest-palindromic-subsequence/description/

C++:

class Solution {
public:
int longestPalindromeSubseq(string s)
{
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n));
for (int i = n - 1; i >= 0; --i)
{
dp[i][i] = 1;
for (int j = i + 1; j < n; ++j)
{
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]);
}
}
}
return dp[0][n - 1];
}
};

参考:http://www.cnblogs.com/grandyang/p/6493182.html

516 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. [LeetCode] Longest Palindromic Subsequence 最长回文子序列

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

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

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

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

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

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

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

  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. 1. Longest Palindromic Substring ( 最长回文子串 )

    要求: Given a string S, find the longest palindromic substring in S. (从字符串 S 中最长回文子字符串.) 何为回文字符串? A pa ...

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

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

随机推荐

  1. linux下的文件和文件夹的权限问题

    1 文件和文件夹的权限 文件和文件夹的权限设置的根本目的是控制人对它们的访问. 2 用户分类 本文件的拥有者.本文件所属的grou.其它用户. 3 也就是说 在读写文件或者文件夹时,要看看自己是属于哪 ...

  2. 新手必备的SEO优化工具

  3. 正则_action

    http://wiki.ubuntu.org.cn/index.php?title=Python%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%93% ...

  4. [TJOI2017] 不勤劳的图书管理员

    题目描述 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生这两本书页数的和的厌烦度.现在有n本被打乱顺序的书, ...

  5. SLG, 菱形格子的算法.(递归版

    class GeoPoint{ public: int x; int y; public: bool operator == (const GeoPoint& p){ return p.x = ...

  6. ERROR: cannot start Android Studio. No JDK found. Please validate either ANDROID_STUDIO_JDK, JDK_HOME + Unrecognized VM option '+UseCodeCacheFlushing

    想学下android,在本来想用myeclipse安装下sdk和adt,谁知在官网看到http://developer.android.com/sdk/index.html Google I/O 20 ...

  7. spring-jar包详解整理(大合集)

    转:https://blog.csdn.net/weisong530624687/article/details/50888094 spring.jar 是包含有完整发布模块的单个jar 包.但是不包 ...

  8. lstat函数的使用【学习笔记】

    通过lstat函数获取文件的类型的代码如下. #include "apue.h" int main(int argc,char *argv[]) { int i; struct s ...

  9. Unable to instantiate receiver XXXXXX

    运行一个工程的时候时logcat中出现了“Unable to instantiate receiver XX..”. 检查后发现,由于是东拼西凑的代码,所以在Manifest文件里注册了Receive ...

  10. RxJava RxBinding 按钮(Button) 点击(click)

    /********************************************************************* * RxJava RxBinding 按钮(Button) ...