[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 ...
- [luogu 3175] [HAOI2015]按位或(min-max容斥+高维前缀和)
[luogu 3175] [HAOI2015]按位或 题面 刚开始你有一个数字0,每一秒钟你会随机选择一个[0,2^n-1]的数字,与你手上的数字进行按位或运算.问期望多少秒后,你手上的数字变成2^n ...
- “laravel.log” could not be opened: failed to open stream
百度了一下,说是要赋权限,按照操作赋了权限也还是报错,其实只要执行第一个就好,但为了保险起见,我都执行了,还是不行 chmod -R /storage chmod -R /storage/logs c ...
- Python字符串和正则表达式中的反斜杠('\')问题
在Python普通字符串中 在Python中,我们用'\'来转义某些普通字符,使其成为特殊字符,比如 In [1]: print('abc\ndef') # '\n'具有换行的作用 abc defg ...
- js汉字转换为拼音
片段 1 片段 2 gistfile1.txt /* --- description: Pinyin, to get chinese pinyin from chinese. license: MIT ...
- C++ new、delete、namespace关键字。
C++ 中的动态内存分配: C++与C语言分配内存关键字不同,C语言中的动态内存分配是通过 malloc(分配内存) 与 free(释放内存)完成.C++使用new(分配内存) delete(释放内 ...
- 字符串的 Base64 加密和解密
base64加密: FORM encode_base64 USING p_business “字符串string CHANGING p_base64. DATA l_xstring TYPE xstr ...
- varchar nvarchar 设计长度时 设计成 (2^n)-1 的好处
这个问题想说已久就是博没共享出来 首先提出个问题 CREATE TABLE `test` ( `a` ) DEFAULT NULL, ) ENGINE=InnoDB DEFAULT CHARSET=u ...
- EF部分字段修改 自动忽略为null字段
传入一个实体 student(){id = 1,name = "测试" age = null,sex = null} 下面 是修改的方法 public async Task Edi ...
- hostid - 显示当前主机的数字化标识
SYNOPSIS(总览) hostid [-v] DESCRIPTION(描述) 显示当前主机的数字化标识(以十六进制的形式表示). --help 显示帮助信息后退出 --version 输出版本信息 ...