516. Longest Palindromic Subsequence
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".
Approach #1: DP. [Java]
class Solution {
public int longestPalindromeSubseq(String s) {
int len = s.length();
int[][] dp = new int[len+1][len+1];
for (int l = 1; l <= len; ++l) {
for (int i = 0; i <= len - l; ++i) {
int j = i + l - 1;
if (i == j) {
dp[i][j] = 1;
continue;
} else if (s.charAt(i) == s.charAt(j))
dp[i][j] = dp[i+1][j-1] + 2;
else
dp[i][j] = Math.max(dp[i+1][j], dp[i][j-1]);
}
}
return dp[0][len-1];
}
}
Analysis:
This problem is similar with 486. Predict the Winner.
dp[i][j] : the longest palindromic subsequence from i to j.
stage: length of substring.
for len = 1 to n:
for i = 0 to n-len:
j = i + len - 1;
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]);
ans : dp[0][len-1].
Approach #2: optimization. [C++]
class Solution {
public:
int longestPalindromeSubseq(string s) {
int len = s.length();
vector<int> dp0(len, 0);
vector<int> dp1(len, 0);
vector<int> dp2(len, 0);
for (int l = 1; l <= len; ++l) {
for (int i = 0; i <= len - l; ++i) {
int j = i + l - 1;
if (i == j) {
dp0[i] = 1;
continue;
} else if (s[i] == s[j]) {
dp0[i] = dp2[i+1] + 2;
} else {
dp0[i] = max(dp1[i+1], dp1[i]);
}
}
dp0.swap(dp1);
dp2.swap(dp0);
}
return dp1[0];
}
};
Reference:
http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-516-longest-palindromic-subsequence/
516. Longest Palindromic Subsequence的更多相关文章
- LN : leetcode 516 Longest Palindromic Subsequence
lc 516 Longest Palindromic Subsequence 516 Longest Palindromic Subsequence Given a string s, find th ...
- 516. Longest Palindromic Subsequence最长的不连续回文串的长度
[抄题]: Given a string s, find the longest palindromic subsequence's length in s. You may assume that ...
- [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- LC 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最大回文子序列
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 ...
- 516 Longest Palindromic Subsequence 最长回文子序列
给定一个字符串s,找到其中最长的回文子序列.可以假设s的最大长度为1000. 详见:https://leetcode.com/problems/longest-palindromic-subseque ...
- 【leetcode】516. Longest Palindromic Subsequence
题目如下: 解题思路:很经典的动态规划题目,但是用python会超时,只好用C++了. 代码如下: class Solution { public: int longestPalindromeSubs ...
- [LeetCode] Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
随机推荐
- 检测IE浏览器版本是否过低
<script type="text/javascript"> /*判断浏览器版本是否过低*/ $(document).ready(function() {s var ...
- SpringBoot简单理解
SpringBoot 一.特点:1.默认大于配置,不需要大量配置文件,没有web.xml,拥有可运行的Application类. 2.一般通过java代码配置,而尽量少使用xml配置. 3.maven ...
- codeforces:MEX Queries分析和实现
首先说明一下MEX,设S是自然数集合N的一个子集,那么S的MEX则为min(N\S),即不包含于S的最小自然数. 题目大意是存在一个空集S,提供n组输入(n<10^5),每组输入对应下面的一个指 ...
- DSOframer 的简单介绍和资源整理(2015-09-02重新整理)
DSOframer 是微软提供一款开源的用于在线编辑 Word. Excel .PowerPoint 的 ActiveX 控件.国内很多著名的 OA 中间件,电子印章,签名留痕等大多数是依此改进而来的 ...
- Golang之反射(重点!!)
1.反射:可以在运行时动态获取变量的相关信息 Import(“reflect”) 两个函数: reflect.TypeOf()//获取变量的类型,返回reflect.Type类型reflect.Val ...
- MyBatis 实用篇(二)配置文件
MyBatis 实用篇(二)配置文件 一.全局配置 全局配置:http://www.mybatis.org/mybatis-3/zh/configuration.html <?xml versi ...
- SNMP++ 编译记录
/************************************************************** 技术博客 http://www.cnblogs.com/itdef/ ...
- 解决root@localhost's password:localhost:permission denied,please try again
背景:在装完Hadoop及jdk之后,在执行start-all.sh的时候出现 root@localhost's password:localhost:permission denied,please ...
- CSS中float和Clear的使用
CSS中float和Clear的使用 本文和大家重点讨论一下CSS中Float和Clear属性的使用,一个float对象可以居左或居右,一个设置为float的对象,将根据设置的方向,左移或右移到其父容 ...
- 一起做RGB-D SLAM(7) (完结篇)
第七讲 添加回环检测 2016.11 更新 把原文的SIFT替换成了ORB,这样你可以在没有nonfree模块下使用本程序了. 回环检测的阈值作出了相应的调整. 请以现在的github上源码为准. 简 ...