200_longest-palindromic-substring
/*
@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的更多相关文章
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- leetcode--5. Longest Palindromic Substring
题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- No.005:Longest Palindromic Substring
问题: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- Leetcode Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【leedcode】 Longest Palindromic Substring
Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...
- [LeetCode_5] Longest Palindromic Substring
LeetCode: 5. Longest Palindromic Substring class Solution { public: //动态规划算法 string longestPalindrom ...
- 5. Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- leetcode-【中等题】5. Longest Palindromic Substring
题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...
- Leetcode5:Longest Palindromic Substring@Python
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
随机推荐
- Digital Tutors - Introduction to Scripting Shaders in Unity 学习笔记
1. Overview 2. Understanding Shader definition:code that define what the material can do in the envi ...
- KoaHub平台基于Node.js开发的Koa的模板系统handlebars插件代码详情
koahub-handlebars koahub-handlebars koahub handlebars templates Installation $ npm install koahub-ha ...
- SEO-关键词密度与友情链接交换技巧
关键词密度 关键词密度 关键词密度与关键词频率所阐述的实质上是同一个概念,用来量度关键词在网页上出现的总次数与其他文字的比例,一般用百分比表示.相对于页面总字数而言,关键词出现的频率越高,关键词密度也 ...
- Servlet 学习简介
一.Servlet简介 Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的 ...
- Postman使用教程——调试网络接口的凶器
postman是谷歌浏览器的一个插件,干什么用的呢?跟题目一样,就是用来调试网络接口的.在我们程序猿做程序的时候,如果做网络应用的开发,比如一些B/S.C/S,我们总会给别人一些网络接口,也会使用别人 ...
- 关于label和span设置width无效问题解决方法
转:http://www.jb51.net/web/113507.html 大家可能不知道默认情况下label.span 设置width 是无效的,只有当display:block时,我们所设置的wi ...
- JavaWeb之多语言国际化
这周打算把国际化.JDBC和XML学习一下,从下周就开始学习三大框架,再坚持一个半月吧就能入门JavaWeb了,上周周末两天过的真是生不如死,两天坐在家里,醒来就写博客,原本在公司也自己操作了一遍,其 ...
- windows phone 8.1常用启动器实例
---恢复内容开始--- 小梦今天给大家分享一下windows phone 8.1常用启动器实例,包括: 电话启动器 短信启动器 邮件启动器 添加约会|备忘到日历 地图启动器 地图路线启动器 wind ...
- CSS -- 练习之制作简单商品图
只加深了印象,出错点:未给左侧人物大图宽高,致使第二行图层叠在其上: <!DOCTYPE html> <html lang="en"> <head&g ...
- Python快速入门(3)
数据结构: 列表的元素可变,用[] or list()创建. 元祖的元素不可变,用() or tuple()创建. 集合的元素不可重复,用{} or set()创建. 字典的存放K-V,用dict() ...