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 ...
随机推荐
- 简单聊聊HTTP/TCP/IP协议
经过几天的面试,有很多公司的面试官都会问到是否了解HTTP/TCP/IP协议? 一遇到这个问题,就一脸懵逼,虽然是计算机基层的东西,看来是必须得了解的,回到家之后,就查找了一些资料,整理了一篇博客,简 ...
- 警惕!MySQL成数据勒索新目标
据最新报道显示,继MongoDB和Elasticsearch之后,MySQL成为下个数据勒索目标,从2月12日凌晨开始,已有成百上千个开放在公网的MySQL数据库被劫持,删除了数据库中的存储数据,攻击 ...
- 【Java集合类】ArrayList详解 (JDK7)
相信对于使用过Java的人来说,ArrayList这个类大家一定不会陌生. 数据结构课上讲过, Array是数组,它能根据下标直接找到相应的地址,所以索引速度很快,但是唯一的缺点是不能动态改变数组的长 ...
- ThreadLocal笔记
1.ThreadLocal的作用是什么? ThreadLocal是一个泛型类,将保存在其中的值与当前的线程关联起来,这样每个线程看到的值对于其他线程来说都是不可见的,这个技术被称为线程封 ...
- Java面试14|Session与Cookie
1.在分布式环境,管理Session通常使用下面三种方式: (1)Session Replication 方式管理 (即session复制) 将一台机器上的Session数据广播复制到集群中其余机器上 ...
- Cookie和Session的原理图
Cookie Session
- C++ IO学习
关于IO,主要有这么三种类型:标准输入输出,文件输入输出,字符串流.后面两种都是继承自第一种标准输入输出的.他们分别对应的头文件是: 标准输入输出:#include <iostream> ...
- Java学习笔记 11/15:一个简单的JAVA例子
首先来看一个简单的 Java 程序. 来看下面这个程序,试试看是否看得出它是在做哪些事情! 范例:TestJava.java // TestJava.java,java 的简单范例 public ...
- Android开发之NavigationView的使用
NavigationView主要是和DrawerLayout框架结合使用,来完成抽屉导航实现侧边栏 引用一段官方文档的示例代码 <android.support.v4.widget.Drawer ...
- UT源代码123
(3)设计佣金问题的程序 commission方法是用来计算销售佣金的需求,手机配件的销售商,手机配件有耳机(headphone).手机壳(Mobile phone shell).手机贴膜(Cellp ...