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. SPOJ 703 SERVICE - Mobile Service 题解

    题面 好题啊!~ 设f[i][j][k][l]表示已经处理完前i个请求后,a在j,b在k,c在l的最小值是多少: 那么f[i][p[i]][k][l]=min(f[i][p[i]][k][l],f[i ...

  2. ZOJ 2836 Number Puzzle 题解

    题面 lcm(x,y)=xy/gcd(x,y) lcm(x1,x2,···,xn)=lcm(lcm(x1,x2,···,xn-1),xn) #include <bits/stdc++.h> ...

  3. py3.7安装Scrapy及安装时的 Running setup.py install for Twisted ... error 和安装后的 Unhandled error in Deferred:

    1.首先,win+r 进入cmd,打开命令提示符,输入  pip install scrapy  等待自动安装: 2.到了后半段会出现  Running setup.py install for Tw ...

  4. cmd打开指定目录技巧

    在win的搜索栏直接打上“cmd”后回车 输入cmd 结果:

  5. Git_初步了解

    Git入门篇 一:Git是什么?Git是目前世界上最先进的分布式版本控制系统.工作原理 / 流程: Workspace:工作区Index / Stage:暂存区Repository:仓库区(或本地仓库 ...

  6. Spring Cloud 入门概括介绍

    出处: 拜托!面试请不要再问我Spring Cloud底层原理 概述 毫无疑问,Spring Cloud是目前微服务架构领域的翘楚,无数的书籍博客都在讲解这个技术.不过大多数讲解还停留在对Spring ...

  7. Homebrew学习(六)之替换及重置homebrew、Homebred Core、Homebrew cask默认源

    替换及重置homebrew默认源 中科大源 替换官方源: // 替换brew.git: cd "$(brew --repo)" git remote set-url origin ...

  8. ORA-01846: 周中的日无效

    参考这篇博客:https://blog.csdn.net/yabingshi_tech/article/details/8678218

  9. vue-cli常用插件安装教程

    1.安装sass npm i sass-loader node-sass --save-dev 2.安装stylus cnpm install stylus --save-dev cnpm insta ...

  10. 日语能力考试N2级核心词汇必备—副词

                                                                         日语能力考试N2级核心词汇必备—副词 ABAB型的副词 あちこ ...