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. Torch 两个矩形框重叠面积的计算 (IoU between tow bounding box)

    Torch 两个矩形框重叠面积的计算 (IoU between tow bounding box) function DecideOberlap(BBox_x1, BBox_y1, BBox_x2, ...

  2. mybatis动态传入表名、列名

    原文:http://luoyu-ds.iteye.com/blog/1517607 要实现动态传入表名.列名,需要做如下修改 添加属性statementType=”STATEMENT” (可省略) 同 ...

  3. Lintcode27-Reverse 3-digit Integer

    Reverse a 3-digit integer. Example Example 1: Input: number = 123 Output: 321 Example 2: Input: numb ...

  4. Python: 字典应用题

    Write a program to read through the mbox-short.txt and figure out who has sent the greatest number o ...

  5. HDU 4318 Power transmission(最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=4318 题意: 给出运输路线,每条路线运输时都会损失一定百分比的量,给定起点.终点和初始运输量,问最后到达终点时最 ...

  6. JaveWeb 公司项目(6)----- 通过ToolTip给控件添加动态注释

    现在公司的项目进展到了视屏这一块,关于海康网页端的构建我会另外写一篇博客来详细讲解,这一篇的博文主要讲的是我刚刚遇到的一个小问题 连接上了视屏之后,将控制按钮换成图标,方位按钮比较好理解,调焦调距的按 ...

  7. 关于python的基础知识

    一,编程语言的类型: 1.编译型 2.解释型 3.静态语言 4.动态语言 5.强类型定义语言 6.弱类型定义语言 编译型vs解释型 编译型: 优点:编译器一般会有预编译的过程对代码进行优化.因为编译只 ...

  8. [osg]osg窗口显示和单屏幕显示

    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile("cow.osg"); osg::ref_ptr&l ...

  9. C语言中的多线程编程

    很久很久以前,我对C语言的了解并不是很多,我最早听说多线程编程是用Java,其实C语言也有多线程编程,而且更为简单.方便.强大.下面就让我们简单领略一下Unix C语言环境下的多线程编程吧! 下面先看 ...

  10. 力扣(LeetCode) 104. 二叉树的最大深度

    给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], ...