题目描述:

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

解题分析:

之前百度实习生面试也被问到这个问题。这个题比较简单。就是在分别考虑对称字符字串为奇数和偶数的情况,在不超过字符串范围的情况下向两边扩展,找到最大值,并记录下最大子串的位置,最后返回即可。

具体代码:

public class Solution {
public static String longestPalindrome(String s) {
if(s==null)
return null;
if(s.length()==0)
return "";
if(s.length()==1)
return s;
if(s.length()==2){
if(s.charAt(0)!=s.charAt(1)){
return s.substring(1);
}
else
return s;
}
int max=1;
int start=0;
int end=0;
char[] array=s.toCharArray();
//对称字符字串长度为数的情况
for(int i=1;i<s.length()-1;i++){
int from=i;
int to=i;
// 要保证不超过字符数组边界的情况下,待扩展的两字符相等
while(from>=0&&to<s.length()&&array[from]==array[to]){
from--;
to++;
}
int len=to-from-1;
if(len>max){
start=from+1;
end=to-1;
max=len;
}
}
//对称字符字串长度为偶数的情况
for(int i=0;i<s.length()-1;i++){
if(array[i]!=array[i+1])
continue;
int from=i;
int to=i+1;
while(from>=0&&to<s.length()&&array[from]==array[to]){
from--;
to++;
}
int len=to-from-1;
if(len>max){
start=from+1;
end=to-1;
max=len;
}
}
return s.substring(start,end+1);
}
}

【leetcode】5. Longest Palindromic Substring的更多相关文章

  1. 【LeetCode】5. Longest Palindromic Substring 最长回文子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...

  2. 【LeetCode】5. Longest Palindromic Substring 最大回文子串

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  3. 【LeetCode】005. Longest Palindromic Substring

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  4. 【一天一道LeetCode】#5 Longest Palindromic Substring

    一天一道LeetCode系列 (一)题目 Given a string S, find the longest palindromic substring in S. You may assume t ...

  5. 【LeetCode】516. Longest Palindromic Subsequence 最长回文子序列

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...

  6. 【leetcode】516. Longest Palindromic Subsequence

    题目如下: 解题思路:很经典的动态规划题目,但是用python会超时,只好用C++了. 代码如下: class Solution { public: int longestPalindromeSubs ...

  7. 【SP1812】LCS2 - Longest Common Substring II

    [SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...

  8. 【SP1811】LCS - Longest Common Substring

    [SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...

  9. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

随机推荐

  1. Codeforces Round #328 (Div. 2) B. The Monster and the Squirrel 打表数学

    B. The Monster and the Squirrel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/c ...

  2. POJ1463:Strategic game(树形DP)

    Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot f ...

  3. thinkphp中的分表方法

    public function getPartitionTableName($data=array()) { // 对数据表进行分区 if(isset($data[$this->partitio ...

  4. UNIX标准化及实现之POSIX标准扩展头文件

    POSIX标准定义的XSI(X/Open System Interface)扩展头文件 头文件 说明 <cpio.h> cpio归档值 <dlfcn.h> 动态链接 <f ...

  5. 如何用jquery操作table的方法

    今天我在做你约我吧交友www.niyuewo.com网项目时遇到一个问题,就是如何用qjuery控制table的添加.编辑与删除,经过网上查资料发现用jquery很容易实现,在此整理下来供大家参考: ...

  6. C语言中堆和栈的区别

    原文:http://blog.csdn.net/tigerjibo/article/details/7423728 C语言中堆和栈的区别 一.前言: C语言程序经过编译连接后形成编译.连接后形成的二进 ...

  7. java_接口和抽象类的区别

    1. 接口只能定义抽象方法,不包含已经提供实现的方法. 抽象类可以包含普通方法 2. 接口不能定义静态方法.抽象类可以定义静态方法 3. 接口里只能定义静态常量filed,不能定义普通filed. 抽 ...

  8. win7 debian 双系统修改引导项顺序

    转载:http://jingyan.baidu.com/article/72ee561aa1d123e16138df81.html 问题描述: 个人在宿舍使用的比较多的是Window 7,而它的启动项 ...

  9. visual studio 2013 c++ 打开code map 功能

    属性->c++ -> Browse Infomation -> Enable Browse Infomation设为true http://msdn.microsoft.com/li ...

  10. 无法挂载 “7.9 GB Filesystem”.

    有个8G的U盘,格式化成exfat格式.插入电脑后点击盘符,弹出错误提示: 无法挂载 “7.9 GB Filesystem”. Error mounting: mount exited with ex ...