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

Runtime: 128 ms, faster than 59.09% of C++ online submissions for Longest Palindromic Subsequence.
Memory Usage: 90.5 MB, less than 0.79% of C++ online submissions for Longest Palindromic Subsequence.
 
 
class Solution {
public:
int longestPalindromeSubseq(string s) {
int N = s.size();
vector<vector<int>> dp(N, vector<int>(N,));
for(int i=; i<N; i++) {
dp[i][i] = ;
}
for(int gap=; gap<s.size(); gap++) {
for(int i=; i<N; i++) {
int j = i+gap;
if(i+gap >= N) continue;
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-];
}
};

LC 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. 516. Longest Palindromic Subsequence

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

  4. [LeetCode] 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. WMware Workstation——网络类型:NAT、bridge、host-only

    bridge NAT host-only 总结: NAT和host-only都需要虚拟网卡(WMware Adapter Card)实现物理机和虚拟机互访. NAT和bridge都可以访问外网 NAT ...

  2. 报错:ipython 6.5.0 has requirement prompt-toolkit<2.0.0,>=1.0.15, but you'll have prompt-toolkit 2.0.15 which is incompatible.

    pip install imagededup 时,报错:ipython 6.5.0 has requirement prompt-toolkit<2.0.0,>=1.0.15, but y ...

  3. 用js刷剑指offer(栈的压入、弹出序列)

    题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压 ...

  4. Luogu P1294 高手去散步

    Luogu P1294 高手去散步 因为数据较小,所以用邻接矩阵存图即可. 将1号点到$t$号点分别设为起点,深搜遍历路线,开一个$vis$数组,记录每一个点是否被访问过.每次求出从当前起点出发的最大 ...

  5. IDEA实用教程(五)——配置IDEA的JVM内存值

    ---恢复内容开始--- 四. 配置IDEA的JVM内存值 IDEA默认配置的JVM内存值比较低,如果硬件配置较高,可以修改该设置. 该设置需要在工程界面进行. 该操作仅建议内存8G以上,64位操作系 ...

  6. [Ynoi2017]由乃的OJ

    题意 由乃正在做她的OJ.现在她在处理OJ上的用户排名问题.OJ上注册了n个用户,编号为1-",一开始他们按照编号 排名.由乃会按照心情对这些用户做以下四种操作,修改用户的排名和编号:然而由 ...

  7. Django REST framework+Vue 打造生鲜电商项目(笔记五)

    一.viewsets实现商品详情页接口 (1)商品详情页只需要多继承一个类(mixins.RetrieveModelMixin)就可以了,(它的功能就是展示商品详情) class GoodsListV ...

  8. 查看工程里有多少行java代码

    /** * @Classname CustBankcardServiceImpl * @Description 计算项目代码量(行数) * @Date 2019/11/04 14:24 * @Crea ...

  9. LibreOJ #115. 无源汇有上下界可行流

    二次联通门 : LibreOJ #115. 无源汇有上下界可行流 /* LibreOJ #115. 无源汇有上下界可行流 板子题 我也就会写写板子题了.. */ #include <cstdio ...

  10. 搭建自己的博客(八):使用fontawesome框架来添加图标以及美化详情页

    在网页中有时候会使用到图标,自己弄图标有些麻烦所以就使用了fontawesome框架. 官网:   下载地址 我使用的fontawesome版本是5.5.0版本 1.先上变化的部分