LeetCode Algorithm 05_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.
Tags: String
容易想到的思路(可以解但会超时):
class Solution {
public:
string longestPalindrome(string s) {
int len = s.length();
for(int n=len; n>=; n--){
for(int i=; i<=len-n; i++){
if(isPalin(s.substr(i,n))){
return s.substr(i,n);
}
}
}
}
bool isPalin(string sub){
int len = sub.length();
for(int i=,j=len-; i<j; i++,j--){
if(sub[i]!=sub[j])
return false;
}
return true;
}
};
更好的思路:
上述思路相当于先构建一系列子字符串,然后分析每个子串是否回文数,这样将会有非常多的组合,计算复杂度高。这种思路其实是收缩的思路。
如果我们从前往后遍历,每次遍历到一个字符便以之为中心看能向两边扩散多远,从而形成一些列子串,如果形成的子串长度大于result,则更新result。这种思路是扩散的思路。
此题其实与LeetCode Algorithm 03有类似之处,即都不能直接构造固定长度字符串然后判断是否合乎要求。正确的做法都是先设定一个子串的出发点(端点或中心点),然后尽可能增长直到不满足条件,形成子串,然后更新原有的局部最佳子串,最终形成全局最长子串。
c++代码:
class Solution {
public:
string centerToOutside(string s, int left, int right){
while(left>= && right<=s.length()- && s[left]==s[right]){
left--;right++;
}
return s.substr(left+, right-left-);
}
string longestPalindrome(string s) {
int len = s.length();
if(len == ){
return "";
}
string result = s.substr(,);
for(int i=; i<len-; i++){
string s1 = centerToOutside(s, i, i);
if(s1.length() > result.length()){
result = s1;
}
string s2 = centerToOutside(s, i, i+);
if(s2.length() > result.length()){
result = s2;
}
}
return result;
}
};
LeetCode Algorithm 05_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 ...
- 【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][Python]Longest Palindromic Substring
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- [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
写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...
- LeetCode 5 Longest Palindromic Substring(最长子序列)
题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- [LeetCode] 5. Longest Palindromic Substring ☆☆☆☆
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
随机推荐
- 51nod 多重背包问题(二进制优化)
有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放在容量为W的背包里,每种物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2......Pn(Pi ...
- PHP保留两位小数
1.不四舍五入 $number = 23.43453;$english_format_number = number_format($number, 2, '.', '');echo $english ...
- [Python] Python Libs
The Python Standard Library has a lot of modules! To help you get familiar with what's available, he ...
- 作为刚開始学习的人应该怎样来学习FPGA
FPGA作为一种高新的技术.已经逐渐普及到了各行各业.不管是消费类.通信类.电子行业都无处不在它的身影,从1985年第一颗FPGA诞生至 今,FPGA已经历了将近20多个年头,从当初的几百个门电路到如 ...
- 用ElasticSearch,LogStash,Kibana搭建实时日志收集系统
用ElasticSearch,LogStash,Kibana搭建实时日志收集系统 介绍 这套系统,logstash负责收集处理日志文件内容存储到elasticsearch搜索引擎数据库中.kibana ...
- CentOS 开启 IPV6
编辑网卡地址:#vi /etc/sysconfig/network-scripts/ifcfg-eth0IPV6INIT=yesIPV6FORWARDING=yesIPV6ADDR=2607:9000 ...
- jQuery源码04 data() : 数据缓存
/* Implementation Summary 1. Enforce API surface and semantic compatibility with 1.9.x branch 2. Imp ...
- 一题多解(三)—— Python 字符串的拼接
1. format def event_log(name, time): print('Event: {}, happens at {}'.format(name, str(time))) 2. 使用 ...
- 83.const与类
const常量对象,无法改变数据,只能引用尾部带const方法 类的成员如果是const,可以默认初始化,也可以构造的初始化,不可在构造函数内部初始化 类中的const成员,无法直接修改,可以间接修改 ...
- java为什么要定义接口等相关解释
1.接口的作用是实现多重继承 因为只能继承一个类(规定的) 2.一个类只能继承一个父类,但是可以实现一个或多个接口 3.abstract关键词能让你在类里创建一个或多个没有定义的方法—你给出接口,但 ...