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 ...
随机推荐
- HTML表格表单综合——用户注册表
今天学习了表格和表单知识,我综合了他们添加了一些拓展知识做了一个用户注册表,以下面代码来整理表格和表单知识: <!DOCTYPE html PUBLIC "-//W3C//DTD XH ...
- vue-cli webpack在node环境下安装使用
第一步,需要下载并安装node.js以及他的npm组件: 第二步,用node -v;npm -v来测试node.js以及npm是否安装成功(建议用GIT命令行工具,因为GIT是linux系统),如果显 ...
- 关于XAMPP环境配置
关于XAMPP软件 * Apache - 软件服务器(运行PHP) * 启动失败 * 原因 - 端口号被占用 * 错误信息 - Error: Apache shutdown unexpectedly ...
- Log4j2分析与实践
当前网络上关于Log4j2的中文文章比较零散,这里整理了一下关于Log4j2比较全面的一些文章,供广大技术人员参考 Log4j2分析与实践-认识Log4j2 Log4j2分析与实践-架构 Log4j2 ...
- [Leetcode] Binary search, DP--300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- django使用model创建数据库表使用的字段
Django通过model层不可以创建数据库,但可以创建数据库表,以下是创建表的字段以及表字段的参数.一.字段1.models.AutoField 自增列= int(11) 如果没有的话,默认会生成一 ...
- 快来领取一场专门讲解UTF-8与UTF-16编码算法的GitChat活动的免费名额
微信扫一扫,可打开该GitChat活动页面 字符编码是计算机世界里最基础.最重要.最令人困惑的一个主题之一.不过,在计算机教材中却往往浮光掠影般地草草带过,甚至连一本专门进行深入介绍的专著都找不到(对 ...
- Linux中samba服务器的搭建
使用的vmware12虚拟机安装的centos6.8和物理机上的windows10,实现在windows10 上访问CentOs上的samba服务. 一.先查看系统中是否安装有samba服务相关的软件 ...
- 关于MATLAB处理大数据坐标文件2017624
经过一个下午找资料,已作出人工轨迹记录程序,这样就可以增加样本容量 接下来三个方向:特征.决策树.机械轨迹程序 虽然机械轨迹的程序相对好做,但是现有机械轨迹程序太过于死板,不能自行更改轨迹
- Win10 & Linux Docker 安装使用
Docker最近推出了可以运行在Win10和Mac上的稳定版本,让我们赶紧来体验一下. 一.Windows Docker 安装 1.安装 需要的条件为: 64bit Windows 10,开启Hype ...