/*
@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. iOS热更新技术被苹果官方警告?涉及到RN、Weex、JSPatch!!!

    今天一早,不少iOS开发群都炸窝了,原因是部分iOS开发者收到了苹果的警告邮件: 有开发者质疑可能是项目中使用了JSPatch.weex以及ReactNative等热更新技术.对于修复bug提交审核的 ...

  2. koahub.js 0.09 发布,新增钩子机制

    koahubjs发布0.09 新增钩子机制添加钩子机制,控制器钩子和函数钩子修复自动加载bug,实现除自动加载导出的default外,还能自动加载其他的方法记koahubjs钩子开发过程在使用koah ...

  3. struts2(一) struts2入门

    首先推荐一本书,虽然我还没看过,但是我以后肯定会看的,<Struts+技术内幕>提取密码:kg6w .现在只是停留在会使用struts2的层次,自己也想继续深入研究,但是感觉自己的知识面还 ...

  4. i++和++i 的学习

    在<<C++ Primer>> 第四版 5.5中讲解到: ++i:返回操作之后的结果,且是个左值. i++:返回操作之前的结果,且是有右值. // 2_3.cpp : 定义控制 ...

  5. java利用url实现网页内容的抓取

    闲来无事,刚学会把git部署到远程服务器,没事做,所以简单做了一个抓取网页信息的小工具,里面的一些数值如果设成参数的话可能扩展性能会更好!希望这是一个好的开始把,也让我对字符串的读取掌握的更加熟练了, ...

  6. paoding-rose 了解

    paoding-rose 是人人开源的基于 spring 开发的 javaEE 框架.wiki 地址: https://code.google.com/archive/p/paoding-rose/ ...

  7. Knockoutjs : Unable to process binding "value:

    刚刚自学knockoutjs,老是碰到稀奇古怪的问题. 在自学knockout.js的时候经常遇到 Unable to process binding "value:的问题.目前总结了以下几 ...

  8. yii2.0使用之缓存

    1.片段缓存(针对于视图中的某部分进行缓存): <?php 设置有效时间 $time=15; 缓存依赖,存入文件.当文件内容发生改变是才会刷新新内容 $dependecy=[ 'class'=& ...

  9. iOS开发之URLSession

    1.概述 n  NSURLSession是iOS7中新的网络接口,与NSURLConnection是并列的. n  当程序在前台时,NSURLSession与NSURLConnection大部分可以互 ...

  10. [AndroidTips]startService与bindService的区别

    Service的生命周期方法比Activity少一些,只有onCreate, onStart, onDestroy我们有两种方式启动一个Service,他们对Service生命周期的影响是不一样的. ...