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.

要求:给定一个字符串,求最长的回文字符串

"a"
=>a
"runcodes"
=>r
"runcodestatus"
=>tat
"runcodestats"
=>stats

public String longestPalindrome(String s) {
s = s.trim();
if(null==s || "".equals(s))
return null;
String res = s.substring(0,1);
String temp;
for(int i=0; i<s.length()-1; i++){
temp = search(s, i, i);
if(temp.length()>res.length())
res = temp;
temp = search(s, i, i+1);
if(temp.length()>res.length())
res = temp;
}
return res;
}
//这个方法向 i 的两端发散
public String search(String s,int begin,int end){
while(begin>=0 && end<s.length() && (s.charAt(begin)==s.charAt(end))){
begin--;
end++;
}
return s.substring((begin+1),end);
}

分析:以每个字符为中心,遍历前后的字符串

[LeetCode]-algorithms-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. 求最长回文子串 - leetcode 5. Longest Palindromic Substring

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

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

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

  5. 【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 ...

  6. leetcode:Longest Palindromic Substring(求最大的回文字符串)

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

  7. [LeetCode][Python]Longest Palindromic Substring

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

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

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

  9. [LeetCode] 5. Longest Palindromic Substring 最长回文子串

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

  10. 最长回文子串-LeetCode 5 Longest Palindromic Substring

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

随机推荐

  1. 洛谷 P2051 中国象棋 题解

    题面 状态可能不太好想,设f[i][j][k]表示前i行其中有j行是放一个炮,有k行是放两个炮的合法方案数: 那么: f[i+1][j][k]+=f[i][j][k]     在这一行不放任何棋子: ...

  2. <<C++ Primer>> 术语表 (总) (待补充)

    术语表 目录 第 1 章 开始 第 I 部分 C++基础 第 2 章 变量和基本类型 第 3 章 字符串, 向量和数组 第 4 章 表达式 第 5 章 语句 第 6 章 函数 第 7 章 类 第 II ...

  3. noip2011day2-观光公交

    题目描述 风景迷人的小城 \(Y\) 市,拥有 $n $个美丽的景点. 由于慕名而来的游客越来越多,\(Y\) 市特 意安排了一辆观光公交车,为游客提供更便捷的交通服务. 观光公交车在第 \(0\) ...

  4. PHP trait与单例模式 (一次编写,到处使用)

    一  trait php是单继承的语言,无法同时从两个基类中继承属性和方法,为了解决这个问题,php出了Trait这个特性. 个人理解的trait是: trait = abstract class - ...

  5. drf模块分析

    drf请求模块.渲染模板.解析模块.响应模块.异常模块 请求模块 drf的请求模块 1.drf的request是在wsgi的request基础上再次封装 2.wsgi的request作为drf的req ...

  6. python 中if __name__ = '__main__' 的作用

    python 中if __name__ = '__main__' 的作用 前言 首先我们要知道在python里面万物皆对象,模块也是对象,并且所有的模块都有一个内置属性 __name__. 一个模块的 ...

  7. java Class类的用法示例

    @SuppressWarnings("unchecked") public void func() throws InstantiationException, IllegalAc ...

  8. Python操作Redis,你要的都在这了!

    Redis是一个基于内存的高效的键值型非关系型数据库,存取效率极高,而且支持多种存储数据结构,使用也非常简单.本节中,我们就来介绍一下Python的Redis操作,主要介绍RedisPy这个库的用法. ...

  9. Git 查看远端仓库地址

    git remote -v

  10. Nginx优化_访问并发量(进程可以打开的最大文件数量)

    如果客户端访问服务器提示“Too many open files”如何解决? [root@proxy ~]# ab -n 2000 -c 2000 http://192.168.1.100/    # ...