/*
@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. 深入理解 JavaScript 异步系列(2)—— jquery的解决方案

    第一部分,jQuery-1.5 之后的 ajax 本地址http://www.cnblogs.com/wangfupeng1988/p/6515779.html未经允许不得转载~ $.ajax这个函数 ...

  2. APUE学习心得

    APUE学习心得 Chapter 3 IO 3.2 文件描述符 文件描述符是一个非负整数.当打开 一个现存文件或创建一个新文件时,内核向进程返回一个文件描述符.0 标准输入,1 标准输出, 2 标准错 ...

  3. AVFoundation之如何从摄像头获取图像

    前言: 最近项目有个需求是对试图对手机密码进行强破解的人进行拍照(通过摄像头截图),因为之前没做过,所以一堆坑.现在就把我的经验都分享出来,希望后来人不用再踏上坑途中. 直接上代码: // 创建会话 ...

  4. Java synchronized 关键字的实现原理

    数据同步需要依赖锁,那锁的同步又依赖谁?synchronized给出的答案是在软件层面依赖JVM,而Lock给出的方案是在硬件层面依赖特殊的CPU指令,大家可能会进一步追问:JVM底层又是如何实现sy ...

  5. 安卓Android的内存管理原理解析

    Android采取了一种有别于Linux的进程管理策略,有别于Linux的在进程活动停止后就结束该进程,Android把这些进程都保留在内存中,直到系统需要更多内存为止.这些保留在内存中的进程通常情况 ...

  6. 【Troubleshooting Case】Unable to delete Exchange database?

    在我们日常邮件系统运维管理或实施部署变更中,经常会遇到,删除Exchange 数据库DB时,提示无法删除. ------------------– Microsoft Exchange Error - ...

  7. Asp.NetCore1.1版本没了project.json,这样来生成跨平台包

    本章将要和大家分享的是Asp.NetCore1.1版本去掉了project.json后如何打包生成跨平台包, 为了更好跟进AspNetCore的发展,把之前用来做netcore开发的vs2015卸载后 ...

  8. yii2.0自带email

    大部分框架都有自带的email邮件发送类,yii的邮件发送也很简单,代码如下: 修改配置文件,普通版在(config/web.php).高级版默认配置在/common/config/main-loca ...

  9. pcntl_fork 导致 MySQL server has gone away 解决方案

    pcntl_fork 前连数据库,就会报 MySQL server has gone away 错误.原因是子进程会继承主进程的数据库连接,当mysql返回数据时,这些子进程都可以通过这个连接读到数据 ...

  10. flask mega-tutorial 1.0 documentation学习记录

    本文主要是记录在[用户登录]一节中出现的问题: 报错位置是在 if g.user is not None and g.user.is_authenticated(): return redirect( ...