1. Question

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

2. Solution

  1. 动态规划。依次求出所有长度的子字符串的最长回文子序列。

  2. dp[i][i + j] = max(s[i] == s[i + j] ? dp[i + 1][i + j - 1] + 2 : dp[i + 1][i + j - 1], max(dp[i + 1][i + j], dp[i][i + j - 1])); 其中i表示起点,j表示子字符串长度。

3. Code

class Solution {
public:
int longestPalindromeSubseq(string s) {
// dp
int len = s.length();
vector<vector<int>> dp(len, vector<int>(len, 1)); for (int j = 1; j < s.length(); j++) {
for (int i = 0; i < s.length() - j; i++) {
dp[i][i + j] = max(dp[i + 1][i + j], dp[i][i + j - 1]);
if (s[i] == s[i + j]) {
if (i + 1 <= i + j - 1) {
dp[i][i + j] = max(dp[i][i + j], dp[i + 1][i + j - 1] + 2);
} else
dp[i][i + j] = max(dp[i][i + j], j + 1);
} else {
if (i + 1 <= i + j - 1)
dp[i][i + j] = max(dp[i][i + j], dp[i + 1][i + j - 1]);
}
}
}
return dp[0][s.length() - 1];
}
};

LeetCode——Longest Palindromic Subsequence的更多相关文章

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

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

  2. [Leetcode] Longest Palindromic Subsequence

    Longest Palindromic Subsequence 题解 题目来源:https://leetcode.com/problems/longest-palindromic-subsequenc ...

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

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

  5. LN : leetcode 516 Longest Palindromic Subsequence

    lc 516 Longest Palindromic Subsequence 516 Longest Palindromic Subsequence Given a string s, find th ...

  6. [leetcode]516. Longest Palindromic Subsequence最大回文子序列

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

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

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

  8. [Swift]LeetCode516. 最长回文子序列 | Longest Palindromic Subsequence

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

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

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

随机推荐

  1. Storm-源码分析- timer (backtype.storm.timer)

    mk-timer timer是基于PriorityQueue实现的(和PriorityBlockingQueue区别, 在于没有阻塞机制, 不是线程安全的), 优先级队列是堆数据结构的典型应用 默认情 ...

  2. Django 框架搭建入门案例

    1. 什么是 web 框架 对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端; # 示例: import socket def handle_re ...

  3. ntopng基础

    当你在本地网络监控网络流量,根据流量大小.监控平台/接口.数据库类型等等,可以有许多不同的选择.ntopng是一套开源(遵循GPLv3协议)网络流量分析解决方案,提供基于web界面的实时网络流量监控. ...

  4. Apache2.4部署python3.6+django2.0项目

    一.安装apache Apache是非常有名的web服务器软件,如果想让我们web项目运行几乎离不开它. Apache官方网站:http://httpd.apache.org/ 根据自己的环境,选择相 ...

  5. System.ArgumentException: 字体“Courier New”不支持样式“Regular”。

    使用MongoVUE,发现报错,报错信息如下: System.ArgumentException: 字体“Courier New”不支持样式“Regular”. 说明本机字体安装不够:需安装完整的Co ...

  6. 哈密顿绕行世界问题---hdu2181(全排列问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2181 题意很容易理解,dfs就可以了 #include <iostream> #inclu ...

  7. C. Mail Stamps---cf29c(离散化,图)

    题目链接:http://codeforces.com/problemset/problem/29/C 题意就是有n(1e5)个点,找到一条能把所有的点都包含在内的路径,由于点的编号是 1e9 所以不得 ...

  8. mysql 复合查询语句

    INSERT INTO runwa(rshottime,rmoney,renamecount) VALUES (CURDATE(),(select SUM(MONEY) from income whe ...

  9. iOS学习之Objective-C 2.0 运行时系统编程

    0 导言 本主主要内容包括: 1.概述2.参考3.运行时系统的版本和平台4.和运行时系统的交互5.消息6.动态方法解析7.消息转发8.类型编码9.属性声明 1 概述 Objective-C语言将决定尽 ...

  10. 设备驱动与控制器 I/O

    控制器是对硬件发起控制命令,负责给系统提供接口,想要正常使用该硬件功能系统中必须安装相应驱动 I/O设备 cpu和存储器并不是操作系统唯一需要管理的资源,I/O设备也是非常重要的一环. I/O设备一般 ...