需求: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. 基于NopCommerce的开发框架——缓存、网站设置、系统日志、用户操作日志

    最近忙于学车,抽时间将Nop的一些公用模块添加进来,反应的一些小问题也做了修复.另外有园友指出Nop内存消耗大,作为一个开源电商项目,性能方面不是该团队首要考虑的,开发容易,稳定,代码结构清晰简洁也是 ...

  2. 作为前端,我为什么选择 Angular 2?

    转自:https://sanwen8.cn/p/2226GkX.html 没有选择是痛苦的,有太多的选择却更加痛苦.而后者正是目前前端领域的真实写照.新的框架层出不穷:它难吗?它写得快吗?可维护性怎样 ...

  3. 加载jquery插件注意了

    1.尽量放在</body>之前,不要放在</head>标签之前,如果执意要放也要放在css之后,例如: <link href="style.css" ...

  4. js事件循环

    之前有看过一些事件循环的博客,不过一阵子没看就发现自己忘光了,所以决定来自己写一个博客总结下! 首先,我们来解释下事件循环是个什么东西: 就我们所知,浏览器的js是单线程的,也就是说,在同一时刻,最多 ...

  5. java之真假分页

    真分页(要的是什么范围的记录在数据库查的时候就只查这几条记录):select s.* from (select *,row_number() over(order by SLoginId) as ro ...

  6. 走进BFC

    在解释 BFC 是什么之前,需要先介绍 Box.Formatting Context的概念. Box: CSS布局的基本单位: Box 是 CSS 布局的对象和基本单位, 直观点来说,就是一个页面是由 ...

  7. lvs之 lvs原理架构介绍

    一. 概念 lvs的术语: Router:GWIP vs:virtual server,director rs:real server CIP:client IP VIP:virtual server ...

  8. 卷积神经网络的变种: PCANet

    前言:昨天和大家聊了聊卷积神经网络,今天给大家带来一篇论文:pca+cnn=pcanet.现在就让我带领大家来了解这篇文章吧. 论文:PCANet:A Simple Deep Learning Bas ...

  9. EF编辑

    //修改推荐的信息 var productRe = db.Shop_ProductRecommends.Single(item => item.Id == model.Id); productR ...

  10. head first python helloword

    如何使用python 打出hello word 在python shell 键入print 'hello word'( python 2) or  print ("hello word&qu ...