[leetcode]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".
* 子序列不一定连续,双指针,写出状态方程就好写了,二维数组res[sta][end]代表回文的开头和结尾坐标分别是sta和end的长度*/
public int longestPalindromeSubseq(String s) {
int len = s.length();
int[][] res = new int[len][len];
return len(0,len-1,res,s);
}
public int len(int sta,int end,int[][] res,String s)
{
//由于max函数中要求两个长度,两次求值过程中为了避免重复工作,可以直接利用res中已经存在的值,可以提高很多效率
if (res[sta][end] != 0)
return res[sta][end];
if (sta > end)
return 0;
if (sta == end)
return 1;
//状态方程:
//if[s(sta) == s(end)]:d[sta][end] = d[sta+1][end-1] + 2;
//else[s(sta) != s(end)]:d[sta][end] = max[d[sta+1][end],d[sta][end-1]]
if (s.charAt(sta) == s.charAt(end))
{
res[sta][end] = len(sta+1,end-1,res,s) + 2;
}
else
{
res[sta][end] = Math.max(len(sta+1,end,res,s),len(sta,end-1,res,s));
}
return res[sta][end];
}
[leetcode]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 ...
- leetcode 5 Longest Palindromic Substring--最长回文字符串
问题描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- [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] 409. Longest Palindrome 最长回文
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 【LeetCode】516. Longest Palindromic Subsequence 最长回文子序列
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...
- 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 最长回文子序列
给定一个字符串s,找到其中最长的回文子序列.可以假设s的最大长度为1000. 详见:https://leetcode.com/problems/longest-palindromic-subseque ...
- 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 ...
随机推荐
- 「考试」CSP-S 2020
乱扯 爆炸的过程是这样的 写了\(2.5h\)的\(T1\)过不去大样例,自闭了 决定调\(T2\)然后过了样例但事实上写的完全是假的 这个时候突然\(T1\)灵光一闪就没再看\(T2\)了 然后就一 ...
- 面经手册 · 第20篇《Thread 线程,状态转换、方法使用、原理分析》
作者:小傅哥 博客:https://bugstack.cn Github:https://github.com/fuzhengwei/CodeGuide/wiki 沉淀.分享.成长,让自己和他人都能有 ...
- 第5.3节 详说Python风格的函数分配参数
一. 分配参数的定义 参数收集就是在定义函数时不能确认参数个数,用收集参数将调用时不确定数量的实参存放到收集参数的元组中.分配参数与此过程相反,它不是在定义函数形参时使用星号(1个或2个),而是 ...
- moviepy音视频剪辑VideoClip类set_position方法pos参数的使用方法及作用
☞ ░ 前往老猿Python博文目录 ░ moviepy音视频剪辑VideoClip类set_position方法用于多个剪辑合成一个剪辑时设置调用剪辑实例的拷贝在合成剪辑的位置. 调用语法: set ...
- element ui的el-radio踩坑
1.html 1 <div class="listPeopleDetail"> 2 <div class="item" v-for=" ...
- 重庆聚焦区块链应用,Panda Global觉得春天真的来了!
近日,由2020中国智博会组委会主办.重庆市大数据应用发展管理局与渝中区人民政府联合承办.重庆市区块链应用创新产业联盟和四川省区块链行业协会联合执行的"2020线上智博会区块链应用创新大赛& ...
- rsync+inotify-tools实时备份脚本
1.1 实时备份 1.需求分析: 为什么要实时复制 因为nfs是单点非常的不安全 而通过定时任务备份会造成数据丢失 这是就需要需要实时备份 2实时方案 1).搭建好服务端backup与客户端nfs的 ...
- Angular:惰性加载的模块
①通过ng new angular-module创建一个全新的angular应用,默认不选路由 ②通过一下命令分别创建2个模块和1个组件 ng g m hx1 ng g c hx1 ng g m hx ...
- Linux下yum下载依赖包
先安装依赖包yum-plugin-downloadonly [root@node1 ~]# yum install yum-plugin-downloadonly 方法一: [root@node1 ~ ...
- Difference between @Bean and @Autowired
Demo01 1 @SpringBootApplication 2 public class Application { 3 4 @Autowired 5 BookingService booking ...