需求: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. a标签实现文件下载

    如果想通过纯前端技术实现文件下载,直接把a标签的href属性设置为文件路径即可,如下: <a href="https://cdn.shopify.com/s/files/1/1545/ ...

  2. 【数据库】Mean web开发 02-Windows下Mongodb安装配置及常用客户端管理工具

    简介 Mean是JavaScript的全栈开发框架.更多介绍 用MongoDB实现持久数据的存储是Mean Web全栈开发中的一部分. MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非 ...

  3. openresty使用笔记(一)

    背景介绍 游戏经过一段时间的运营,发现了原来的设计缺陷太多,所以决定重新设计架构.使用到nginx作为核心并通过lua+redis设计实现自己的负载分配方案.先看看下面这张简单的架构图吧~ 从图上看, ...

  4. Iterator invalidation(迭代器失效)

    一.vector 所有读操作.swap.std::swap:都不会引起迭代器失效... clear.operator=.assign:都会引起全部变量迭代器失效 reserve.shrink_to_f ...

  5. Win7安装Docker

    系统环境 1. windows 7 旗舰版64位 i5-2450M CPU  8G内存 2.支持“ Hardware Virtualization Technology”,并且,“virtualiza ...

  6. 编写原生JS的insertAfter函数

    DOM里有insertBefore函数,但没有insertAfter函数,所以自己编写一个该函数: function insertAfter(newElement, targetElement){ v ...

  7. Java基础——继承

    学习Java继承之前,我们想回忆一下Java面向对象需要特别注意的几个关键点. 面向对象是将复杂的事情简单化了,它通过封装的方式使得代码的重用性更高和安全性更强.平时我们要学会用面向对象的方式去思考, ...

  8. 解决Ubuntu SMPlayer播放视频无声音问题

    问题:Ubuntu Kylin 14.04 系统默认装好之后,smplayer播放视频都是正常的,但最近可能由于一些误设置,导致smplayer播放任何格式的视频都无声.解决方法:由于ALSA是Lin ...

  9. [0] C#实现WebBrowser&HTML交互

    using System;using System.ComponentModel;using System.Windows.Forms; namespace WindowsApplication5{ ...

  10. thinkphp 单字母函数

    在ThinkPHP中有许多使用简便的单字母函数(即快捷方法),可以很方便开发者快速的调用,但是字母函数却不方便记忆,本文将所有的字母函数总结一下,以方便以后查找. 1.U() URL组装 支持不同UR ...