/*
@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. 使用js在网页上记录鼠标划圈的小程序

    Spin-Wheel 实现鼠标在网页上转圈时记录转动圈数的小程序,每转一圈记录一次,同时要是顺时针方向的. 问题分析与实现 这个小程序的难点在于如何知道鼠标完成了一个转圈的动作,而且人工使用鼠标划圈时 ...

  2. Jmeter+Badboy实战经验二(使用jmeter)

    1. 新建线程组: TestPlan--添加--Threads(Users)--线程组

  3. webots自学笔记(二)节点与机器人建模

    原创文章,出自"博客园, _阿龙clliu" :http://www.cnblogs.com/clliu/ 上一次介绍了界面和一个简单的自由落体,然而在实际运用中,机器人的结构都是 ...

  4. 每天一个Linux命令 7

    常用yum命令1)查询 yum list #查询所有可用软件包列表yum search 关键字 #搜索服务器上所有和关键字相关的包2)安装 yum -y install 包名选项: install 安 ...

  5. 1113: [Poi2008]海报PLA

    1113: [Poi2008]海报PLA Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 765  Solved: 466[Submit][Status ...

  6. Spring IOC容器中Bean的生命周期

    1.IOC容器中Bean的生命周期 构造器函数 设置属性 初始化函数(在Bean配置中 init-method) 使用Bean 结束时关闭容器(在Bean中配置destroy-method) 2.Be ...

  7. Gridview 重建表头/单击单元格弹出对话框/改变单元格背景色

    整理工作~ 完整的代码在GitHub上, 路径: 项目背景:追踪某个issue,并且记录每天的状态. 要求:1.点击日期就能更改,并且用颜色标志不同的状态 2.增加按钮可关闭issue 3.布局要求日 ...

  8. python调用SOA服务

    python调用SOA服务,运用suds模块 #! /usr/bin/python # coding:gbk import suds,time,sys reload(sys) sys.setdefau ...

  9. css兼容问题 ie6,7

    H5标签兼容 元素浮动之后能设置宽度的话就给元素加宽度,如果需要元素宽度是内容撑开,就给他里面的块元素加上浮动 第一块加浮动,第二块加margin等于第一块元素在IE6下会有间隙问题 IE6下子元素超 ...

  10. JetBrains套装免费学生授权申请(IntelliJ, WebStorm...)

    IntelliJ作为一款强大的Java开发IDE,售价自然也不会低.但是对于学生或教师来说,JetBrains开发工具免费提供给学生和教师使用.而且取得一次授权后只需要使用相同的 JetBrains ...