5. Longest Palindromic Substring最大回文子串
int sta = 0;
int max = 1;
public String longestPalindrome(String s) {
/*
判断回文有两种:
1.最大回文子序列求长度:
用动态规划,dp[sta][end] 代表开头为sta、结尾为end的部分最大回文子序列的长度是多少
dp[sta][end] = (s.charAt(sta)==s.charAt(end))?dp[sta+1][end-1]+2:max(dp[sta][end-1],dp[sta+1][end])
2.最大回文子串:
用两遍延伸法,分为两种情况,奇数子串和偶数子串:
把所有字符当做中轴,遍历一遍,每当长度超过,就更新结果
*/
int l = s.length();
if (l==0)
return "";
for (int i = 0; i < l; i++) {
//奇数情况
helper(s,i,i);
//偶数情况
helper(s,i,i+1);
}
//这里注意是前闭后开区间,注意区间
return s.substring(sta,sta+max);
}
public void helper(String s,int left,int right){
//从中轴向两边延伸,注意最后的结果多了一次
while (left >= 0&& right < s.length()&& s.charAt(left)==s.charAt(right))
{
left--;
right++;
}
//由于left多减了一次,right多加了一次,所以处理要注意
if (max < right-left-1)
{
max = right-left-1;
sta = left+1;
}
}
最大回文子序列在:http://www.cnblogs.com/stAr-1/p/7444994.html
5. Longest Palindromic Substring最大回文子串的更多相关文章
- 【LeetCode】5. Longest Palindromic Substring 最大回文子串
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- Longest Palindromic Substring-----最长回文子串
首先讲讲什么是回文, 看看Wiki是怎么说的:回文,亦称回环,是正读反读都能读通的句子.亦有将文字排列成圆圈者,是一种修辞方式和文字游戏.回环运用得当.能够表现两种事物或现象相互依靠或排斥的关系, 比 ...
- leetcode 5 Longest Palindromic Substring--最长回文字符串
问题描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- 【LeetCode-面试算法经典-Java实现】【05-Longest Palindromic Substring(最大回文字符串)】
背景 近期開始研究算法,于是在leetcode上做算法题,第五题Longest Palindromic Substring便是关于回文子串的. 什么是回文字串 回文字符串是指将该字符串前后颠倒之后和该 ...
- [leetcode]516. Longest Palindromic Subsequence最大回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- Manacher's Algorithm && 647. Palindromic Substrings 计算回文子串的算法
注:转载自:https://www.cnblogs.com/love-yh/p/7072161.html
- C++ leetcode Longest Palindromic Substring
明天就要上课了,再过几天又要见班主任汇报项目进程了,什么都没做的我竟然有一种迷之淡定,大概是想体验一波熬夜修仙的快乐了.不管怎么说,每天还是要水一篇博文,写一个LeetCode的题才圆满. 题目:Gi ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- [译+改]最长回文子串(Longest Palindromic Substring) Part II
[译+改]最长回文子串(Longest Palindromic Substring) Part II 原文链接在http://leetcode.com/2011/11/longest-palindro ...
随机推荐
- The First Assignment
我的第一条随笔 ========== 这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/SE2020-2 这个作业要求在哪里 https://edu.cn ...
- centOs7.5.64之前的操作系统搭建GitLab记录
GitLab搭建步骤: 1. Install and configure the necessary dependencies (1)yum install curl openssh-server o ...
- 2、Spring Cloud和dubbo简介
1.Spring Cloud简介 (1).Spring Cloud简介 SpringCloud,基于SpringBoot提供了一套微服务解决方案,包括服务注册与发现,配置中心,全链路监控,服务网关,负 ...
- 基于CFSSL工具创建CA证书,服务端证书,客户端证书
背景描述 在局域网中部署组件时,想要通过证书来实现身份的认证,确保通信的安全性,可以通过cfssl工具来进行CA证书,服务端证书,客户端证书的创建. 目录 背景描述 部署cfssl工具 下载,上传cf ...
- Splay树求第k大模板
今天上午借着休息日得机会手撸了一下模板,终于对着模板调出来了.prev和next占用了std namespace里面的东西,然后报警我上次给关了所以.....就花了3个小时吧. inline加不加无所 ...
- PyQt(Python+Qt)学习随笔:QScrollArea滚动区域的alignment属性
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 滚动区域的alignment属性对应QScrollArea的alignment属性,用于控制滚动区域 ...
- Python正则表达式re.findall("[A-Za-z]([A-Za-z0-9])*[.]txt",'Abc2019.txt')的结果为什么是['9']
在<Python妙用re.sub分析正则表达式匹配过程>中老猿分析了findall函数的返回情况,老猿前一阵子在执行这个语句时: >>> re.findall(" ...
- PyQt(Python+Qt)学习随笔: QAbstractItemView的dragDropMode属性
老猿Python博文目录 老猿Python博客地址 一.概述 dragDropMode属性用于控制视图拖放事件的处理方式,其类型为枚举类型DragDropMode. 二.枚举类型DragDropMod ...
- leetcode——(四)2020.06.08
新的一周,leetcode计划:78,79,98,102,236,124,128 (23)
- 冲刺Day7
每天举行站立式会议照片: 昨天已完成的工作: 1.确认商品分类栏,并前后端交互 2.检查.更正订单模块的代码 3.检查.更正用户模块的代码 今天计划完成的工作: 成员 任务 高嘉淳 检查代码.提供测试 ...