需求: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 - 字符串中最长的回文字段的更多相关文章

  1. python经典算法题:求字符串中最长的回文子串

    题目 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab" 注意: ...

  2. c++ 获取字符串中最长的回文子串

    #include <vector> #include <iostream> #include <string> using namespace std; strin ...

  3. leetcode 5 :Longest Palindromic Substring 找出最长回文子串

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  4. 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为1000。

    给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &quo ...

  5. LeetCode 5 Longest Palindromic Substring manacher算法,最长回文子序列,string.substr(start,len) 难度:2

    https://leetcode.com/problems/longest-palindromic-substring/ manacher算法相关:http://blog.csdn.net/ywhor ...

  6. 【LeetCode】5. Longest Palindromic Substring 最长回文子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...

  7. LeetCode 第五题 最长的回文字符串 (JAVA)

    Longest Palindromic Substring 简介:字符串中最长的回文字符串 回文字符串:中心对称的字符串 ,如 mom,noon 问题详解: 给定一个字符串s,寻找字符串中最长的回文字 ...

  8. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  9. leetcode:Longest Palindromic Substring(求最大的回文字符串)

    Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...

随机推荐

  1. poj2104(划分树模板)

    poj2104 题意 给出一个序列,每次查询一个区间,要求告诉这个区间排序后的第k个数. 分析 划分树模板,O(mlogn). 建树.根据排序之后的数组,对于一个区间,找到中点的数,将整个区间分为左右 ...

  2. NLTK学习笔记(一):语言处理和Python

    目录 [TOC] nltk资料下载 import nltk nltk.download() 其中,download() 参数默认是all,可以在脚本里面加上nltk.download(需要的资料库) ...

  3. Maven基本安装与配置

    百度Maven,进入Maven官网,点击Download 点击下载Binary zip包,下载到电脑上相应的位置即可. 找到下载文件,进行解压,解压到相应的文件夹下面,并且记住路径. 打开系统-> ...

  4. cpp(第十一章)

    1. std::istream & operator>>(std::istream &is,complex_c &t) { std::cout<<&qu ...

  5. weather API 天气api接口 收集整理

    腾讯 http://sou.qq.com/online/get_weather.php?callback=Weather&city=南京 中国天气-weather.com.cn http:// ...

  6. django ngRoute ui-router 开发环境下禁用缓存

    问题描述: Python manage.py runserver ,禁用缓存,及时修改反馈到浏览器 解决办法: 使用dummy cache: Dummy caching (for developmen ...

  7. PostgreSQL数据库web维护客户端工具软件

    TreeSoft数据库管理系统使用JAVA开发,采用稳定通用的springMVC +JDBC架构,实现基于WEB方式对 MySQL,Oracle,PostgreSQL 等数据库进行维护管理操作. 功能 ...

  8. 百度地图 js api 实现 line 居中显示

    项目中有个需求需要在百度地图的中心显示画的线,以前用过mapPanto这个方法,传入坐标就可以将地图平移到这个坐标,不过不知道如何获取线的中心点,看了别人的代码,有以下两个函数可以实现这个功能 get ...

  9. el表达式字符串使用总结

    el表达式截取 逗号后面的字符串${fn:substringAfter(strVar,',' )} el表达式判断字段长度<c:if test="${fn:length(strVar) ...

  10. Appcan开发笔记:结合JQuery的$.Deferred()完善批量异步发送

    appcan的 uexXmlHttpMgr.send 或者 appcan.ajax无法同步请求(没有找到这个属性),只能异步,造成循环多次提交时由于延迟或网络堵塞等原因无法同步响应,导致提交顺序混乱, ...