LeetCode——Longest Palindromic Subsequence
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
动态规划。依次求出所有长度的子字符串的最长回文子序列。
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的更多相关文章
- [LeetCode] Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- [Leetcode] Longest Palindromic Subsequence
Longest Palindromic Subsequence 题解 题目来源:https://leetcode.com/problems/longest-palindromic-subsequenc ...
- [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- 【LeetCode】516. Longest Palindromic Subsequence 最长回文子序列
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...
- LN : leetcode 516 Longest Palindromic Subsequence
lc 516 Longest Palindromic Subsequence 516 Longest Palindromic Subsequence Given a string s, find th ...
- [leetcode]516. Longest Palindromic Subsequence最大回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- [Swift]LeetCode516. 最长回文子序列 | Longest Palindromic Subsequence
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- 516. Longest Palindromic Subsequence最长的不连续回文串的长度
[抄题]: Given a string s, find the longest palindromic subsequence's length in s. You may assume that ...
随机推荐
- ubuntu android 设备识别 Setting up a Device for Development
参考: http://developer.android.com/tools/device.html lsusb Bus 001 Device 004: ID 18d1:9025 Google I ...
- c#使用FastReports打印
private void btnprint_Click(object sender, EventArgs e) { //报表路径 string path = Application.StartupPa ...
- python基础-第十三篇-13.1web框架本质
基础与概念 众所周知,对于所有的web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端 web框架分两类:一类是包括socket和业务逻辑(tornado),另一 ...
- 并发编程 - 进程 - 1.队列的使用/2.生产者消费者模型/3.JoinableQueue
1.队列的使用: 队列引用的前提: 多个进程对同一块共享数据的修改:要从硬盘读文件,慢,还要考虑上锁: 所以就出现了 队列 和 管道 都在内存中(快): 队列 = 管道 + 上锁 用队列的目的: 进程 ...
- 剑指Offer——不用加减乘除做加法
题目描述: 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. 分析: "^"是不带进位的加法. "&"可以得到所有进位位组 ...
- first-child与:first-of-type的区别
css选择器中:first-child与:first-of-type的区别 :first-child选择器是css2中定义的选择器,从字面意思上来看也很好理解,就是第一个子元素.比如有段代码: p:f ...
- uchome client.php
uchome 主要使用了php的call_user_func()函数,在uc_clinet/client.php中,一般指向uc_api_mysql,而 uc_api_mysql()函数 则负责分发到 ...
- Spark源码分析之Sort-Based Shuffle读写流程
一 .概述 我们知道Spark Shuffle机制总共有三种: 1.未优化的Hash Shuffle:每一个ShuffleMapTask都会为每一个ReducerTask创建一个单独的文件,总的文件数 ...
- 如何用meavn构建mahout项目
(1)下载meavn 解压到D盘
- python全栈开发从入门到放弃之推导式详解
variable = [out_exp_res for out_exp in input_list if out_exp == 2] out_exp_res: 列表生成元素表达式,可以是有返回值的函数 ...