516. Longest Palindromic Subsequence
516. Longest Palindromic Subsequence
- 方法1: DP
- 思路:
- code
https://www.youtube.com/watch?v=_nCsPn7_OgI
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”.
方法1: DP
思路:
DP definition : maintain dp[i][j] table, which keep tracking of the longest palindrome length inside s[i , j]
Initialization : dp[i][i] = 1, i.e. each single character count as 1
Transfer equation :
- 循环式:
for(int l = 2; l <= str.length; l++)
for(int i = 0; i < str.length-l + 1; i++)
let j = i + l - 1
- if (s[i] ==s[j] && l == 2)
dp[i, j] = 2
- else if (s[i] == s[j])
dp[i, j] = 2 + dp[i +i, j -1]
- else
dp[i, j] = max(dp[i, j -1], dp[i + 1, j])
Output : dp[0][n-1]
code
516. Longest Palindromic Subsequence相关推荐
- 【动态规划 回文串11】LeetCode 516. Longest Palindromic Subsequence
LeetCode 516. Longest Palindromic Subsequence 本博客转载自:http://www.cnblogs.com/grandyang/p/6493182.html ...
- leetcode 516. Longest Palindromic Subsequence | 516. 最长回文子序列(递归 -> 傻缓存 ->DP)
题目 https://leetcode.com/problems/longest-palindromic-subsequence/ 题解 1.递归(超时) 递归 -> 傻缓存 ->DP c ...
- C++longest palindromic subsequence最长回文子序列算法实现(附完整源码)
C++longest palindromic subsequence最长回文子序列算法 C++longest palindromic subsequence最长回文子序列算法实现完整源码(定义,实现, ...
- LeetCode Longest Palindromic Substring
原题链接在这里:https://leetcode.com/problems/longest-palindromic-substring/ 题目: Given a string s, find the ...
- [译+改]最长回文子串(Longest Palindromic Substring) Part II
[译+改]最长回文子串(Longest Palindromic Substring) Part II 原文链接在http://leetcode.com/2011/11/longest-palindro ...
- 【回文串1 动态规划 马拉车算法】LeetCode 5. Longest Palindromic Substring
LeetCode 5. Longest Palindromic Substring LeetCode中与回文串相关的免费的题目共有15道(5, 9, 125, 131, 132, 214, 234, ...
- leetcode 5 :Longest Palindromic Substring 找出最长回文子串
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- [LeetCode] Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- HPU第三次积分赛-D:Longest Increasing Subsequence(DP)
Longest Increasing Subsequence 描述 给出一组长度为n的序列,a1,a2,a3,a4...an, 求出这个序列长度为k的严格递增子序列的个数 输入 第一行输入T ...
- [LeetCode]Longest Palindromic Substring题解(动态规划)
Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...
最新文章
- Visual Studio:error MSB8020
- 拆分-洛谷P2745 [USACO5.3]窗体面积Window Area
- C++ concurrency::task实现异步编程(Windows)
- 征稿 | MIUA 2022 医学影像理解与分析会议
- 2017.8.21 弦论 思考记录
- 仪表自定义刻度值_汇总丨工业仪表知识大全和选型参考
- 浅析JSONP-解决Ajax跨域访问问题
- EF4的系列文章,mark一下
- Android开发简单购物app源码,校园购物APP源码(适合新手学习)
- DSP SRIO接口设计
- 【IMX6UL开发板试用体验】上手试用与资源使用
- Unity/C# 随机生成中文名
- 利用matlab来设计FIR滤波器参数
- OSG KML文件解析
- Unity Recorder 插件录屏--简单小记
- html 验证码功能 不区分大小写进行验证。
- Gif表情包如何用视频制作?教你一键快速制作gif表情包
- 【找出耗电大户进行消灭 电池持久有绝招】
- select2的使用
- TensorFlow by Google 使用排序 APIMachine Learning Foundations: Ep #9 - Using the Sequencing APIs