/*
@Copyright:LintCode
@Author:   Monster__li
@Problem:  http://www.lintcode.com/problem/longest-palindromic-substring
@Language: Java
@Datetime: 17-03-07 15:38
*/

public class Solution {
    /**
     * @param s input string
     * @return the longest palindromic substring
     */
    public String longestPalindrome(String s) {
     int s_length=s.length();
     int index_front=0,index_back=1,palindrome_length;//index_back=index_front+palindrome_length-1;
     int i,palindrome_length_max=1;
     int max_index_front=0,max_index_back=0;
  int palindromelength[][]=new int[s_length+2][s_length+2];
  for(index_front=0; index_front<s_length; ++index_front)
    palindromelength[index_front][1] = 1;
  for(palindrome_length=2;palindrome_length<=s_length;palindrome_length++)
  {
   for(index_front=0;index_front<s_length&&(index_back=index_front+palindrome_length-1)<s_length;++index_front)
   {
    if(s.charAt(index_front)==s.charAt(index_back)&&palindromelength[index_front+1][palindrome_length-2]==palindrome_length-2)
     palindromelength[index_front][palindrome_length]=palindromelength[index_front+1][palindrome_length-2]+2;
    else
     palindromelength[index_front][palindrome_length]=Math.max(palindromelength[index_front+1][palindrome_length], palindromelength[index_front+1][palindrome_length-1]);
    if(palindrome_length_max<palindromelength[index_front][palindrome_length])
    {
     palindrome_length_max=palindromelength[index_front][palindrome_length];
     max_index_front=index_front;
     max_index_back=index_back;
    }
   }
  }
  return s.substring(max_index_front, max_index_back+1);
    }
}

200_longest-palindromic-substring的更多相关文章

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

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

  2. leetcode--5. Longest Palindromic Substring

    题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

  3. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  4. No.005:Longest Palindromic Substring

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

  5. Leetcode Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  6. 【leedcode】 Longest Palindromic Substring

    Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...

  7. [LeetCode_5] Longest Palindromic Substring

    LeetCode: 5. Longest Palindromic Substring class Solution { public: //动态规划算法 string longestPalindrom ...

  8. 5. Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  9. leetcode-【中等题】5. Longest Palindromic Substring

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

  10. Leetcode5:Longest Palindromic Substring@Python

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

随机推荐

  1. Collector for ArcGIS的使用体验

    基于Esri的Portal for ArcGIS(下面简称Portal),用户可以搭建一个本地的地理信息云平台.围绕着这个云平台,Esri为不同的终端提供了响应的解决方案,其中Collector fo ...

  2. Oracle Jdbc驱动下载及安装本地maven仓库

    由于二进制许可 binary license的限制,oracle jdbc驱动不能通过共有仓库来获取,所以你可以下载下来添加到自己的本地仓库或私有仓库中. 添加到本地仓库步骤如下: 下载Oracle ...

  3. observe.js 源码 学习笔记

    /** * observejs --- By dnt http://kmdjs.github.io/ * Github: https://github.com/kmdjs/observejs * MI ...

  4. ACM 序号互换

    序号互换 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Dr.Kong设计了一个聪明的机器人卡多,卡多会对电子表格中的单元格坐标快速计算出来.单元格的行坐标是由数字 ...

  5. KoaHub.JS基于Node.js开发的处理和显示日期代码

    moment Parse, validate, manipulate, and display dates      A lightweight JavaScript date library for ...

  6. 算法模板——sap网络最大流 2(非递归+邻接表)

    实现功能:同最大流 1 这里面主要是把前面的邻接矩阵改成了邻接表,相比之下速度大大提高——本人实测,当M=1000000 N=10000 时,暂且不考虑邻接矩阵会不会MLE,新的程序速度快了很多倍(我 ...

  7. Java 开机启动

    1.授权: <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">< ...

  8. 2017年最新chrome必备插件推荐

    1. 上网必备 Speed dial plus新标签页, 直接替换掉chrome自带的毫无新意的新标签页,简洁优美快速,我本人非常喜欢. &amp;lt;img src="https ...

  9. Rabbitmq 性能测试

    背景: 线上环境,出了一起事故,初步定位是rabbitmq server. 通过抓包发现,是有多个应用使用同一台rabbitmq server.并且多个应用使用rabbitmq的方式也不一样.发现有以 ...

  10. 分离数据库时出错:无法对数据库'XXX' 执行删除,因为它正用于复制"的解决方法

    出现的原因是要分离的数据库是一个发布订阅的数据库.因为正在复制,所以无法脱机. 解决办法是停止发布订阅,或者删掉它..再分离.有部分情况是在复制目录下并没有看到发布订阅. 有可能是因为以前建立发布订阅 ...