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 ...
随机推荐
- 搭建免费wifi,嗅探接入该wifi的所有网络信息
环境: 1: create_ap , 搭建免费wifi. 2: wireshark , 嗅探网络信息. 搭建热点 搭建热点: git clone https://github.com/oblique/ ...
- Java IO流之打印流与标准流
一.打印流 1.1打印流特点与构造方法 1)PrintStream和PrintWriter类都提供了一系列重载的print和println方法来输出各种类型的数据. 2)PrintStream和Pri ...
- git使用方法1
1.新建一个“本地仓库” $ git init 2.配置仓库 >告诉git你是谁 git config user.name lnj >告诉git怎么联系你 git config user. ...
- JavaScript概念总结:作用域、闭包、对象与原型链
1 JavaScript变量作用域 1.1 函数作用域 没有块作用域:即作用域不是以{}包围的,其作用域完成由函数来决定,因而if /for等语句中的花括号不是独立的作用域. 如前述,JS的在函数中定 ...
- 使用FlashWavRecorder实现浏览器录制wav音频和上传音频文件,兼容IE8以上浏览器
前言:本项目基于github开源插件实现,该插件使用flash实现,兼容IE8以上浏览器 感谢michalstocki的分享该项目,github项目地址:https://github.com/mich ...
- Ajax&jQuery教案总结
Ajax&jQuery教程总结 目录 第一章 Ajax入门 6 第1讲 传统表单提交存在的问题 6 课程内容 6 1. 问题的引入 6 2. 问题的解决 6 参考进度(0.5课时) 7 第2讲 ...
- Hql没有limit,替换方案
在HQL+MYSQL中不能直接在查询语句中使用LIMIT进行检索,正确方法为: String hql = "from User where id=? order by addDate des ...
- Ajax请求,跨域小坑
今天在上班的时候,被坐在旁边项目经理叫过去问了一个Ajax请求跨域的问题,一开始没理解清楚也还有对这个没有理解的透,后面被打击的要死. 当时的需求是需要测试一个已发布的api接口,需要在本地写测试程序 ...
- ssh别名登录密钥登录
在centos上使用别名和是用密钥登录: vim /root/.ssh/config #输入下列内容 Host * User root #以root登录 ServerAliveInterval ...
- JVM高级特性-三、垃圾收集之判断对象存活算法
一.概述 运行时数据区中,程序计数器.虚拟机栈.本地方法栈都是随线程而生随线程而灭的 因此,他们的内存分配和回收是确定的,在方法或线程结束时就回收.而Java堆和方 法区则是不确定的,程序运行过程中创 ...