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".
这是一道典型的dp题,用dp[][]来存储i,j两个pointer指向的string的一个char, 分别按照相等和不相等来处理即可。
class Solution {
public int longestPalindromeSubseq(String s) {
int length = s.length();
int[][] dp = new int[length][length];
for (int i = ; i < length; i++) {
dp[i][i] = ;
}
for (int subLength = ; subLength <= length; subLength++) {
for (int i = ; i + subLength <= length; i++) {
int j = subLength + i - ;
if (s.charAt(i) == s.charAt(j)) {
dp[i][j] = dp[i + ][j - ] + ;
} else {
dp[i][j] = Math.max(dp[i + ][j], dp[i][j - ]);
}
}
}
return dp[][length - ];
}
}
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 ...
- [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 ...
- 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 Subsequence
Longest Palindromic Subsequence 题解 题目来源:https://leetcode.com/problems/longest-palindromic-subsequenc ...
- LeetCode——Longest Palindromic Subsequence
1. Question Given a string s, find the longest palindromic subsequence's length in s. You may assume ...
- 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 ...
- LC 516. Longest Palindromic Subsequence
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
随机推荐
- PTA 阶乘之和取模
阶乘之和取模 (25 分) 输入正整数n, 计算S = 1!+2!+...+n!的末6位(不含前导0). 这里1<=n<=109. 输入样例: 例如输入: 20 输出样例: 输出: ...
- 27.Python列表(list)、元组(tuple)、字典(dict)和集合(set)详解
本章将会介绍 Python 内置的四种常用数据结构:列表(list).元组(tuple).字典(dict)以及集合(set). 这四种数据结构一但都可用于保存多个数据项,这对于编程而言是非常重要的,因 ...
- HDU 5894 hannnnah_j’s Biological Test ——(组合数)
思路来自于:http://blog.csdn.net/lzedo/article/details/52585170. 不过并不需要卢卡斯定理,直接组合数就可以了. 代码如下: #include < ...
- [CSP-S模拟测试]:序列(二分答案+树状数组)
题目传送门(内部题98) 输入格式 第一行一个整数$n$,第二行$n$个整数$a_1\sim a_n$,第三行$n$个整数$b_1\sim b_n$. 输出格式 一行一个整数表示$\max(r-l+1 ...
- (四)C语言之函数
- win10备忘
你要允许来自未知发布者 http://www.xitonghe.com/jiaocheng/Windows10-7809.html输入法 切换繁体 ctrl+shift+F win10 输入法 htt ...
- 使用jQuery做简单的图片轮播效果
一.本特效主要用到的前端知识点 CSS中绝对定位(absolute)CSS实现垂直居中jQuery中简单的淡入淡出动画效果(fadeIn,fadeOut)定时器(setInterval,clear ...
- 动态执行表不可访问,或在v$session
PLSQL Developer报“动态执行表不可访问,本会话的自动统计被禁止”的解决方案 PLSQL Developer报“动态执行表不可访问,本会话的自动统计被禁止”的解决方案 现象: 第一次用PL ...
- 2019.11.29 SAP SMTP郵件服務器配置 發送端 QQ郵箱
今天群裏的小夥伴問了如何配置郵件的問題,隨自己在sap裏面配置了一個 1. RZ10配置參數 a) 参数配置前,先导入激活版本 执行完毕后返回 b) 输入参数文件DEFAU ...
- RESTful 介绍
什么是RESTful?一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务端交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制. ...