516.Longest Palindromic subsequence---dp
题目链接:https://leetcode.com/problems/longest-palindromic-subsequence/description/
题目大意:找出最长回文子序列(不连续),第5题是最长回文子串。
法一(借鉴):dp,与5题,最长回文子串进行比较,这个dp更难一点。代码如下(耗时51ms):
//dp公式:dp[i,j]=2表示下标i到j的字符串中,最长回文子序列长度是2
//当s[i]=s[j]时,dp[i,j]=dp[i+1,j-1]+2
//当s[i]!=s[j]时,dp[i,j]=max(dp[i+1,j],dp[i,j-1])
public int longestPalindromeSubseq(String s) {
int length = s.length();
int dp[][] = new int[length][length];
for(int i = length - 1; i >= 0; i--) {//从后向前dp计算,不知道为什么。。。
dp[i][i] = 1;
for(int j = i + 1; j < length; j++) {//逐一查看每个字符串中的字符序列是否有回文
if(s.charAt(i) == s.charAt(j)) {
dp[i][j] = dp[i + 1][j - 1] + 2;
}
else {
dp[i][j] = Math.max(dp[i][j - 1], dp[i + 1][j]);
}
}
}
return dp[0][length - 1];
}
dp数组(例子:bbab):
| 0("b") | 1("b") | 2("a") | 3("b") | |
| 0("b") | 1(b) | 2(bb) | 2(bba) | 3(bbab) |
| 1("b") | 1(b) | 1(ba) | 3(bab) | |
| 2("a") | 1(a) | 1(ab) | ||
| 3("b") | 1(b) |
可以从上表看出最后结果在dp[0][3]中。
dp数组填充顺序:从下向上,每次计算都使用左边、下边,左下的数据。
法二(借鉴):记忆性搜索,其实感觉还是dp的思想,但是会比纯dp快。代码如下(耗时39ms):
public int longestPalindromeSubseq(String s) {
return dfs(s, 0, s.length() - 1, new int[s.length()][s.length()]);
}
public static int dfs(String s, int i, int j, int dp[][]) {
if(dp[i][j] != 0) {//如果已经有值,则直接返回,代表记忆性
return dp[i][j];
}
if(i > j) {
return 0;
}
if(i == j) {
return 1;
}
if(s.charAt(i) == s.charAt(j)) {
dp[i][j] = dfs(s, i + 1, j - 1, dp) + 2;
}
else {
dp[i][j] = Math.max(dfs(s, i + 1, j, dp), dfs(s, i, j -1, dp));
}
return dp[i][j];
}
516.Longest Palindromic subsequence---dp的更多相关文章
- 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 ...
- 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 ...
- 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 最长回文子序列
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...
- [leetcode]516. 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 最长回文子序列
给定一个字符串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 ...
随机推荐
- VS中碰到的问题
1.调试的时候,语句已经注释掉了,但是在执行的时候还是运行了(或者某些变量值改变后,程序依然用的之前数据). 右键解决方案-->清理,然后重新生成.
- 题解 P1334 【瑞瑞的木板】
声明:本题解已经与其他题解重合, ### 且存在压行情况. 首先,这个题解是我有了惨痛的教训:全部WA... 先发一个CODE做声明: #include <bits/stdc++.h> / ...
- 【转】大数据分析(Big Data OLAP)引擎Dremel, Tenzing 以及Impala
引自:http://blog.csdn.net/xhanfriend/article/details/8434896 对于数据分析师来说,SQL是主要的语言. Hive为Hadoop提供了支持SQL运 ...
- 【BZOJ2763】飞行路线(最短路)
[BZOJ2763]飞行路线(最短路) 题面 BZOJ Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标 ...
- 【BZOJ2780】【SPOJ】Sevenk Love Oimaster(后缀自动机)
[BZOJ2780][SPOJ]Sevenk Love Oimaster(后缀自动机) 题面 BZOJ 洛谷 题解 裸的广义后缀自动机??? 建立广义后缀自动机建立出来之后算一下每个节点被几个串给包括 ...
- Oracle10g数据泵impdp参数详解--摘自网络
Oracle10g数据泵impdp参数详解 2011-6-30 12:29:05 导入命令Impdp • ATTACH 连接到现有作业, 例如 ATTACH [=作业名]. • C ...
- git untrack file
git update-index should do what you want This will tell git you want to start ignoring the changes t ...
- 配置:heartbeat+nginx+mysqld+drbd高可用笔记(OK)
参考资料:http://www.centoscn.com/CentosServer/cluster/2015/0605/5604.html 背景需求: 使用heartbeat来做HA高可用,并且把 ...
- win32/linux 线程 log
原文 #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef WIN32 #includ ...
- SpringBoot ( 八 ) :RabbitMQ 详解
原文出处: 纯洁的微笑 RabbitMQ 即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用. 消息中间件在互联网公司的使用中越来越多,刚才还看到新闻阿里将Roc ...