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. 协议形式化安全分析 Scyther 并非所有协议可以照抄就搬

    1.Scyther 形式化分析工具可以对协议进行形式化描述,验证协议的机密性和可认证性是否存在安全威胁.在攻击时支持会话轮数无限次执行,同时支持在强安全模型和Delov-Yao模型.在对要形式化分析的 ...

  2. Linux提供哪些功能

    不只是Linux,任何一个OS几乎都具有如下功能. 1.文件IO:读写文件 初级文件IO 2.文件属性:获取文件信息 3.标准IO:读取文件系统函数 4.获取IS系统信息 5.进程环境 6.进程控制 ...

  3. Vue项目打包发布,配置Nginx

    #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...

  4. Java学习笔记——第2篇

    Java程序的基本规则 Java程序的组织形式:纯粹的面向对象的程序设计语言,所以Java程序必须以类的形式存在,类是Java程序的最小程序单位,Java程序不允许可执行语句.方法等成分独立存在,所有 ...

  5. What Does Reconciliation Mean in Accounting

    Account reconciliation is the process of comparing transactions you have recorded using internal rec ...

  6. BCB 如何让Application收到SendMessage发送来的消息

    一般,都是通过添加一个ApplicationEvent组件就可以接收到PostMessage发来的消息,那么如何收到SendMessage发来的消息呢? https://stackoverflow.c ...

  7. plus代码闪光点

    1. 快速变成 String 格式: { stamp: +new Date() } 2. 封装axios请求: axios.get(this.URI, { params: { }, withCrede ...

  8. E:nth-child(n)

    E:nth-child(n) 语法: E:nth-child(n) { sRules } 说明: 匹配父元素的第n个子元素E,假设该子元素不是E,则选择符无效.大理石构件维修 要使该属性生效,E元素必 ...

  9. SpringBoot监控中心

    1.什么是SpringBoot监控中心? 2.为什么要使用SpringBoot监控中心?

  10. SYSTEM_INFORMATION_CLASS

    source: https://github.com/processhacker/processhacker/blob/master/phnt/include/ntexapi.h // rev // ...