Longest Palindromic Substring - 字符串中最长的回文字段
需求:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
如果一个字符串从左向右写和从右向左写是一样的,这样的字符串就叫做palindromic string
判断回文数,中间开花。定一个中心,向两边散开。这个中心是从左到右移动的。需要2个游标。
int palindrome(String ps,int left,int right) 这个方法用来判断回文字段,返回字段长度
String longestPalindrome(String s) 在这里调用palindrome方法,遍历字符串去找。遍历过程注意不要越界。
以下为Java代码:
/**
* @author Rust Fisher
* @date 2015-9-14
*/
public class LongestPalindromicSubstring {
/**
* @param s - input string
* @return the Longest Palindromic Substring
*/
public static String longestPalindrome(String s) {
String result = "";
int inputLenght = s.length();
int startIndex = 0;
int longest = 1;
for (int i = 0; i < inputLenght; i++) {
int oddLen = 1,dualLen = 1, currentLen;
oddLen = palindrome(s, i, i);
if (i+1 < inputLenght) {
dualLen = palindrome(s, i, i+1);
}
currentLen = dualLen > oddLen ? dualLen : oddLen;
if (currentLen > longest){
longest = currentLen;
if (longest%2 == 0) {
startIndex = i - longest/2 + 1;
} else {
startIndex = i - longest/2;
}
}
}
for (int i = startIndex; i < startIndex+longest; i++) {
result += s.charAt(i);
}
return result;
}
/**
* @param ps - input string
* @param left - index move left
* @param right - index move right
* @return the current length of palindrome
*/
public static int palindrome(String ps,int left,int right){
int thislen = 0;
int len = ps.length();
while(left >= 0 && right < len && ps.charAt(left) == ps.charAt(right)){
left--;
right++;
}
thislen = right - left - 1;
return thislen;
}
public static void main(String args[]){
System.out.println(longestPalindrome("hfiwafhaabbaaccddio128213"));
System.out.println(longestPalindrome("abcdefgfedcba"));
System.out.println(longestPalindrome("abc"));
}
}
输出:
aabbaa
abcdefgfedcba
a
Longest Palindromic Substring - 字符串中最长的回文字段的更多相关文章
- python经典算法题:求字符串中最长的回文子串
题目 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab" 注意: ...
- c++ 获取字符串中最长的回文子串
#include <vector> #include <iostream> #include <string> using namespace std; strin ...
- leetcode 5 :Longest Palindromic Substring 找出最长回文子串
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为1000。
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &quo ...
- LeetCode 5 Longest Palindromic Substring manacher算法,最长回文子序列,string.substr(start,len) 难度:2
https://leetcode.com/problems/longest-palindromic-substring/ manacher算法相关:http://blog.csdn.net/ywhor ...
- 【LeetCode】5. Longest Palindromic Substring 最长回文子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...
- LeetCode 第五题 最长的回文字符串 (JAVA)
Longest Palindromic Substring 简介:字符串中最长的回文字符串 回文字符串:中心对称的字符串 ,如 mom,noon 问题详解: 给定一个字符串s,寻找字符串中最长的回文字 ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
随机推荐
- 【Python3之常用模块】
一.time 1.三种表达方式 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.命令如下 ...
- 抓包工具 - HttpWatch
HttpWatch是功能强大的网页数据分析工具,集成在IE工具栏,主要功能有网页摘要.cookies管理.缓存管理.消息头发送/接收,字符查询.POST数据.目录管理功能和报告输出.HttpWatch ...
- 使用React改版网站后的一些感想
文章转载:http://www.jianshu.com/p/8f74cfb146f7 网站是毕业设计的作品,开发这个网站的目的主要用于记录一些笔记,以及聚合一些资讯信息,也算自己在网络世界中的一块静地 ...
- OC对象之旅 weak弱引用实现分析
Runtime学习 -- weak应用源码学习 Runtime源码分析,带你了解OC实现过程.其中参考了大量的大神的代码以及文献,里面也有个人的见解,欢迎拍砖,欢迎交流. 两种常见使用场景 /// ...
- c++ 库函数返回的字符串指针是否需要手动释放
#include <stdio.h> char * tmpnam(char *s); tmpnam函数返回一个不与任何已存在文件同名的有效文件名,如果字符串s不为空,文件名也会写入它.对t ...
- Azure经典门户创建VM,如何设置使用静态IP地址?
使用 Azure 经典管理门户中创建的虚拟机,无法使用静态IP 地址,在管理界面没有该设置.在新的管理门户中虽然有使用静态IP的设置,但是选项是灰色,无法修改,提示错误:This virtual ma ...
- 夜神模拟器与HBuilder连接/cmd运行提示符/执行夜神模拟器命令/执行HBuilder命令
第一步:启动HBuilder和夜神模拟器 第二步:通过运行电脑命令CMD进入(电脑运行命令的快捷键是:windows键+R2.Ctrl键与Alt键之间的那个键就是windows键或者点击左下角开始图标 ...
- 自己积累写的winfrom 操作api 类
引用 类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...
- Vulkan Tutorial 15 Framebuffers
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 我们在前面的章节中已经讨论了很多次framebuffers帧缓冲区,到目前为止我们配 ...
- Bash的条件表达式求值
Bash的条件控制允许两种类型:1)命令的成功或失败 2)逻辑表达式的真假这两种类型都可以通过退出状态($?)来检验,$?=0为真,否则为假 一.命令的成功或失败 通过查看$?值$echo $? 二. ...