问题描述:

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.

解题思路:

遍历方法,时间复杂度为O(n)。首先从字符串的开头位置一直往后遍历,在每次遍历的过程中由该位置向两边扩散,直到找到最长的子回文串为止。同时需要考虑奇字符串和偶字符串的情况。

代码如下:

public class Solution {
public String longestPalindrome(String s) {
int n = s.length();
String longest = s.substring(0, 1); if (s == null || s.length() == 0)
return null;
for (int i = 0; i < n; i++) {
String p1 = expandFromCenter(s, i, i);
if (p1.length() > longest.length())
longest = p1;
String p2 = expandFromCenter(s, i, i + 1);
if (p2.length() > longest.length())
longest = p2;
}
return longest;
} public String expandFromCenter(String s, int c1, int c2) {
int head = c1;
int tail = c2;
int m = s.length(); while (head >= 0 && tail < m && s.charAt(head) == s.charAt(tail)) {
head--;
tail++;
}
return s.substring(head + 1, tail);
}
}

Java [leetcode 5] Longest Palindromic Substring的更多相关文章

  1. LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法

    LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...

  2. Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

    Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...

  3. 【JAVA、C++】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:Longest Palindromic Substring(求最大的回文字符串)

    Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...

  5. 【LeetCode】Longest Palindromic Substring 解题报告

    DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...

  6. 求最长回文子串 - leetcode 5. Longest Palindromic Substring

    写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...

  7. LeetCode 5 Longest Palindromic Substring(最长子序列)

    题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...

  8. leetcode 5 :Longest Palindromic Substring 找出最长回文子串

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

  9. [LeetCode][Python]Longest Palindromic Substring

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

随机推荐

  1. sysconf和pathconf使用

    问题描述:          查看系统运行时的限制值 问题解决: 执行效果: 源代码:

  2. C# 面向对象之概念理解

    什么是对象? <韦氏大词典>中对对象定义: (1)某种可为人所感知的物质. (2)思维.感受或动作所作用的物质或精神体. ----说白了万物皆对象 熟悉的对象描述: 对象就是客观世界中的物 ...

  3. PHP-Phalcon框架中的数据库操作

    > 本文描述了PHP-Phalcon框架中数据库操作方法,主要讨论Phalcon框架的Model组件中的操作方法.更详细的Model介绍请参考:官方文档 1. 连接数据库 在Phalcon框架中 ...

  4. Project Euler 82:Path sum: three ways 路径和:3个方向

    Path sum: three ways NOTE: This problem is a more challenging version of Problem 81. The minimal pat ...

  5. hdu 3863 No Gambling

    #include<stdio.h> int main() { int n; ) { printf("I bet on Oregon Maple~\n"); } ; } ...

  6. linux内核--进程与线程

    http://blog.csdn.net/yusiguyuan/article/details/12154823 在<linux内核设计与实现>中第三章讲解了进程管理,在关于进程和线程的概 ...

  7. HTML5入门6---视频播放按钮

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. ubuntu 下搭建vsftp

    1. 安装:sudo apt-get install vsftpd 2. 我的目的是建立个ftp,专门的账户访问,账户不可以登陆.不允许匿名登陆 3. 更改配置文件/etc/vsftpd.conf l ...

  9. NSDictionary 初始化

    NSDictionary *dic1=[NSDictionary dictionaryWithObject:@"1" forKey:@"a"];         ...

  10. vmware shared holder 虚拟机设置共享目录

    1, 安装 vm-tools http://askubuntu.com/questions/29284/how-do-i-mount-shared-folders-win7-host-in-ubunt ...