leetcode-algorithms-5 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.

Example 1:

Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.

Example 2:

Input: "cbbd"
Output: "bb"

解法1

暴力破解,从最长的长度开始查找所有的子串,检查子串是否为回文串.

class Solution
{
public:
bool checkPalindrome(const std::string &s)
{
int len = s.size();
for (int i = 0; i < len/2; ++i)
{
if (s[i] != s[len -i - 1])
return false;
}
return true;
} string longestPalindrome(string s)
{
std::string pal;
int len = s.size();
for (int i = len; i > 0; --i)
{
for (int start = 0; start + i <= len; ++start)
{
pal = s.substr(start, i);
if (checkPalindrome(pal))
return pal;
}
} return pal;
}
};

时间复杂度: O(n^3).三个嵌套循环,O(n) * O(n) * O(n/2) = O(n^3).

空间复杂度: O(1)

解法2

观察回文串的规律.如:aba,先找到一个字母,然后两边都增加相同字母也是回文串,以此类推,就可以找到最大回文串.还有另种情况是:abba,则要先找到两个相邻是相同的字母.因此关键就是找到这个回文串的中心.

class Solution
{
public:
int palindromeCenter(const std::string &s, int left, int right)
{
while(left >= 0 && right < s.size() && s[left] == s[right])
{
--left;
++right;
} return right - left - 1;
} string longestPalindrome(string s) {
int len = s.size();
if (len <= 0) return ""; int start = 0;
int palindromeLen = 0;
for (int i = 0; i < len; ++i)
{
int len1 = palindromeCenter(s, i, i);
int len2 = palindromeCenter(s, i, i + 1);
int maxLen = len1 > len2 ? len1 : len2;
if (maxLen > palindromeLen)
{
start = i - (maxLen - 1) / 2;
palindromeLen = maxLen;
}
}
return s.substr(start,palindromeLen);
}
};

时间复杂度: O(n^2).

空间复杂度: O(1).

链接: leetcode-algorithms 目录

leetcode-algorithms-5 Longest Palindromic Substring的更多相关文章

  1. 【一天一道LeetCode】#5 Longest Palindromic Substring

    一天一道LeetCode系列 (一)题目 Given a string S, find the longest palindromic substring in S. You may assume t ...

  2. 【LeetCode OJ】Longest Palindromic Substring

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

  3. LeetCode(3)题解: Longest Palindromic Substring

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

  4. 【LeetCode】5. Longest Palindromic Substring 最长回文子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...

  5. 【LeetCode】5. Longest Palindromic Substring 最大回文子串

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

  6. 【leetcode】5. Longest Palindromic Substring

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

  7. leetcode题解 5. Longest Palindromic Substring

    题目: Given a string s, find the longest palindromic substring in s. You may assume that the maximum l ...

  8. LeetCode OJ:Longest Palindromic Substring(最长的回文字串)

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

  9. 【LeetCode】005. 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

    这道题要求的是给你一个string, 如“adcdabcdcba",要求返回长度最大的回文子字符串.这里有两个条件,一是子字符串,而是回文.用纯暴力搜索的话,需要用到O(n^3)的时间,必然 ...

随机推荐

  1. [LightOJ 1341] Aladdin and the Flying Carpet (算数基本定理(唯一分解定理))

    题目链接: https://vjudge.net/problem/LightOJ-1341 题目描述: 问有几种边长为整数的矩形面积等于a,且矩形的短边不小于b 算数基本定理的知识点:https:// ...

  2. ES6模块化操作

    在ES5中我们要进行模块化操作需要引入第三方类库,随着前后端分离,前端的业务日渐复杂,ES6为我们增加了模块化操作.模块化操作主要包括两个方面. export :负责进行模块化,也是模块的输出. im ...

  3. (转载)windows下安装配置Xampp

    XAMPP是一款开源.免费的网络服务器软件,经过简单安装后,就可以在个人电脑上搭建服务器环境.本文为大家介绍Windows中安装XAMPP(Apache+Mysql+PHP)及使用方法及其相关问题的总 ...

  4. shp与json互转(转载)

    转自:http://blog.sina.com.cn/s/blog_673c98be0102v78i.html 对于搞GIS开发的,这2种数据格式太重要了. 一.shp转json 这个要容易些,方法也 ...

  5. React绑定事件动态化的实现方法

    一.什么是绑定事件 1.1 事件 我这里指的事件一般指的是React自带的触发事件,我这里先简单举例几个 onClick //鼠标点击 onMouseEnter //鼠标滑进 onMouseLeave ...

  6. spring controller方法和jstl

    1复杂类型查询:查询条件已经多于一个实体类中的属性 1)可以创建一个类用于组合查询条件 基础类 public class Items { private Integer id; private Str ...

  7. Qt5.QString传参数

    1.函数传参,如果是 QString&类型 的话,不能直接 传入 char* 类型的参数,若是声明成 const QString&类型 的话,就可以 解释:应该是 函数调用的时候 编译 ...

  8. [原][粒子特效][spark]事件action

    深入浅出spark粒子特效连接:https://www.cnblogs.com/lyggqm/p/9956344.html group调用action的地方: 可以看到使用action的可以是出生一次 ...

  9. eclipse中配置maven和创建第一个 Spring Boot Application

    关于Maven的下载.环境变量的配置自行百度,今天记录一下在Eclipse中配置Maven的操作: mvn -v 出现上图说明maven和jdk的要求都达到了(jdk要8.0及以上的版本) 然后在ec ...

  10. 百万并发中间件系统的内核设计看Java并发性能优化

    “ 这篇文章,给大家聊聊一个百万级并发的中间件系统的内核代码里的锁性能优化. 很多同学都对Java并发编程很感兴趣,学习了很多相关的技术和知识.比如volatile.Atomic.synchroniz ...