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 ...
随机推荐
- 基于Python + requests 的web接口自动化测试框架
之前采用JMeter进行接口测试,每次给带新人进行培训比较麻烦,干脆用python实现,将代码和用例分离,易于维护. 项目背景 公司的软件采用B/S架构,进行数据存储.分析.管理 工具选择 pytho ...
- 逆向libbaiduprotect(四)
百度加固libbaiduprotect.so自身对只读字符串进行了加密保护,防止成为破解和逆向的切入口.一般地认为,只要找出这个解密算法就可以对.rodata段的只读字符串进行破解,从而窥探程序的意图 ...
- MySQL优化之表结构优化的5大建议(数据类型选择讲的很好)
殊不知,在N年前被奉为"圣经"的数据库设计3范式早就已经不完全适用了.这里我整理了一些比较常见的数据库表结构设计方面的优化技巧,希望对大家有用. 由于MySQL数据库是基于行(Ro ...
- 关于Python中yield的一些个人见解
# 样例代码def yield_test(n): for i in range(n): yield call(i) print("i=",i) #做一些其它的事情 print(&q ...
- 在Caffe添加Python layer详细步骤
本文主要讨论的是在caffe中添加python layer的一般流程,自己设计的test_python_layer.py层只是起到演示作用,没有实际的功能. 1) Python layer 在caff ...
- 什么是Css Hack?ie6,7,8的hack分别是什么?
针对不同的浏览器写不同的CSS code的过程,就是CSS hack. 示例如下: 1 2 3 4 5 6 7 8 9 10 11 12 #test { width:300px; heig ...
- maven打包 tomcat运行pom配置 或 打成jar包
maven打包 tomcat运行pom配置,同时还需要配置org.apache.tomcat.maven插件,这里省略. <groupId>com.company</groupId& ...
- python核心数据结构之字典
 [TOC ...
- 搭建es6开发与非开发环境babel-browser
前言 最近打算把es6应用到项目中,但是如何在开发环境(浏览器端)直接运行es6?es6已经发布一段时间了,现在大部分是在node.js环境运行,或者通过babel编译之后运行.babel-brows ...
- Vulkan Tutorial 24 Descriptor pool and sets
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Introduction 描述符布局描述了前一章节讨论过的可以绑定的描述符的类型.在 ...