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. 题解——CodeForces 438D The Child and Sequence

    题面 D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input ...

  2. Nand flash code

    (1)流水灯   1>我们来看原理图                                2>datasheet                             3> ...

  3. BZOJ 1064: [Noi2008]假面舞会(dfs + 图论好题!)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1064 题意: 思路: 考虑以下几种情况: ①无环并且是树: 无环的话就是树结构了,树结构的话想一下就 ...

  4. 1 --- Vue 基础指令

    1.vue 指令 1.v-model  主要在表单中使用,文本框.teaxare.单选.下拉 等 2.v-text   文本渲染  类似{{}} 3.v-show  控制Dom显示隐藏   displ ...

  5. JUnit 4 Vs TestNG比较

    JUnit 4和TestNG都是Java中非常受欢迎的单元测试框架.两种框架在功能上看起来非常相似. 哪一个更好? 在Java项目中应该使用哪个单元测试框架? 下面表中概括了JUnit 4和TestN ...

  6. codeforces 15C. Industrial Nim

    题目链接:http://codeforces.com/problemset/problem/15/C $NIM$游戏是次要的,直接异或石头堆就可以了,问题在于给出的石头堆的数量极多. 考虑利用异或的性 ...

  7. git 先创建本地仓库,再关联远程

    之前都是先在GitHub或者bitbucket上创建repo,然后在本地直接git clone下来. 如果一定需要先在本地创建好文件夹,然后再关联远程仓库. 是这样: 1在远程创建仓库这步不变. 2 ...

  8. Python中数据类型

    一.整数 Python可以处理任意大小的整数,当然包括负整数,在Python程序中,整数的表示方法和数学上的写法一模一样,例如:1,100,-8080,0,等等. 计算机由于使用二进制,所以,有时候用 ...

  9. Linux性能测试工具-UnixBench

    ■下载路径: unixbench-5.1.2.tar.gz :http://soft.vpser.net/test/unixbench/ unixbench-5.1.3.tar.gz :http:// ...

  10. 对nginx进行平滑升级

    1.查看服务器当前nginx版本 [root@instance-hwl9ix5l licenses]# nginx -v           #查看版本 nginx: nginx version: n ...