[LeetCode]-algorithms-Longest Palindromic Substring
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的更多相关文章
- LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法
LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...
- Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)
Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...
- 求最长回文子串 - leetcode 5. Longest Palindromic Substring
写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...
- LeetCode 5 Longest Palindromic Substring(最长子序列)
题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...
- 【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 ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- [LeetCode][Python]Longest Palindromic Substring
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 【LeetCode】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
- [LeetCode] 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 ...
随机推荐
- Luogu P1080 [NOIP2012]国王游戏
题目 按\(a_i*b_i\)升序排序即可. 证明考虑交换法. 对于排序后相邻的两个人\(i,j(a_ib_i\le a_jb_j)\),设前面的总的积为\(s\),则当前答案为\(\max(\fra ...
- 解决 Intellij IDEA Cannot Resolve Symbol ‘BASE Decoder’ 问题
最近接盘了用springboot框架搭建的后台,第一次接触java的我就遇上了bug: 因为jdk更新而导致Cannot Resolve Symbol ‘BASE Decoder’ 问题 看了很多网上 ...
- python 3.x报错:No module named 'cookielib'或No module named 'urllib2'
1. ModuleNotFoundError: No module named 'cookielib' Python3中,import cookielib改成 import http.coo ...
- liunx 环境下安装 Eclipse C++
第一步:首先安装JDK 进入JDK官网:https://www.oracle.com/technetwork/java/javase/downloads/index.html 下载对应的jdk 注意 ...
- JavaEE--JSP详解
一.JSP JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它是由Sun Microsystems公司倡导.许多公司参与一起建立的一种 ...
- npm学习(七)之如何发布包、更新发布包、删除发布包
前言 我们经常使用npm来下载别人的模块或者说包,那么我们如何将自己写的模块上传到npm呢? 了解npm政策 在开始之前,最好回顾一下npm的政策,以防您对站点礼仪.命名.许可或其他指导原则有疑问. ...
- selenium谷歌火狐插件安装
1.首先ctrl+r进入终端输入(pip install selenium)进行python安装selenium2.打开百度浏览器进行分别输入geckodriver和Chromedriver对火狐和谷 ...
- vue-cli3.0中使用 postcss-pxtorem
vue.config.js module.exports = { lintOnSave: true, css: { loaderOptions: { postcss: { plugins: [ req ...
- Android判断是debug还是release模式
1.当有些功能不希望在release模式实现时,但是debug模式又需要的时候,就可以对当前版本模式进行判断.如是debug模式则日志输出级别设置为Level.DEBUG,release模式设置为Le ...
- Genymotion模拟器使用camera
1.前言 最近开发react-native的app,上传图片功能需要使用相机,发现Genymotion默认的相机不工作.查看同行的博客解决了,归纳整理一下. 2.步骤 2.1安装Genymotion: ...