给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为1000。

示例 1:

输入: "babad" 输出: "bab" 注意: "aba"也是一个有效答案。

示例 2:

输入: "cbbd" 输出: "bb"

暴力法:

class Solution {
public:
string longestPalindrome(string s)
{
int len = s.size();
int start = 0;
int end = 0;
int MAX = 1;
for(int i = 0; i < len; i++)
{
for(int j = i + 1; j < len; j++)
{
int flag = true;
for(int k = i; k < i + (j - i + 1) / 2; k++)
{
if(s[k] != s[j + i - k])
{
flag = false;
break;
}
}
if(flag)
{
if(j - i + 1 > MAX)
{
start = i;
end = j;
MAX = j - i + 1;
}
}
}
}
string temp = "";
for(int i = start; i <= end; i++)
{
temp += s[i];
}
return temp;
}
};

动态规划:

为了改进暴力法,我们首先观察如何避免在验证回文时进行不必要的重复计算。考虑 “ababa” 这个示例。如果我们已经知道 “bab” 是回文,那么很明显,“ababa” 一定是回文,因为它的左首字母和右尾字母是相同的。

我们给出 P(i,j) 的定义如下:

P(i,j)={true,false,​如果子串Si​…Sj​是回文子串其它情况​

因此,

P(i,j)=(P(i+1,j−1) and Si​==Sj​)

基本示例如下:

P(i,i)=true

P(i,i+1)=(Si​==Si+1​)

这产生了一个直观的动态规划解法,我们首先初始化一字母和二字母的回文,然后找到所有三字母回文,并依此类推…

复杂度分析

  • 时间复杂度:O(n2), 这里给出我们的运行时间复杂度为 O(n2) 。
  • 空间复杂度:O(n2), 该方法使用 O(n2) 的空间来存储表。
class Solution {
public:
string longestPalindrome(string s)
{
int size = s.size();
int start = 0;
int MAX = 0;
vector<vector<bool> > dp(size, vector<bool>(size, false));
//len代表从当前位置开始长度为len的字符串(不算上当前的字符串)
//dp[i][j] == true 表示索引从i到j的子字符串是回文串
for(int len = 0; len < size; len++)
{
for(int i = 0; i < size - len; i++)
{
if(len == 0)
{
dp[i][i] = true;
}
else if(len == 1 && s[i] == s[i + len])
{
dp[i][i + len] = true;
}
else if(len > 1 && s[i] == s[i + len])
{
dp[i][i + len] = dp[i + 1][i + len - 1];
}
else
{
dp[i][i + len] = false;
} if(dp[i][i + len] == true && len >= MAX)
{
MAX = len;
start = i;
}
}
}
string res = "";
for(int i = start; i <= start + MAX; i++)
res += s[i];
return res;
}
};

Leetcode5.Longest Palindromic Substring最长回文字串的更多相关文章

  1. 【LeetCode每天一题】Longest Palindromic Substring(最长回文字串)

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

  2. 5. Longest Palindromic Substring -- 最长回文字串

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

  3. [LeetCode] Longest Palindromic Substring 最长回文串

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

  4. LeetCode-5:Longest Palindromic Substring(最长回文子字符串)

    描述:给一个字符串s,查找它的最长的回文子串.s的长度不超过1000. Input: "babad" Output: "bab" Note: "aba ...

  5. LeetCode5. Longest Palindromic Substring 最长回文子串 4种方法

    题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题意很简单,就是求一个字符串得最长子串,这里的子串指连续的. 本文给 ...

  6. Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

    Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...

  7. [LeetCode] 5. Longest Palindromic Substring 最长回文子串

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

  8. 1. Longest Palindromic Substring ( 最长回文子串 )

    要求: Given a string S, find the longest palindromic substring in S. (从字符串 S 中最长回文子字符串.) 何为回文字符串? A pa ...

  9. LeetCode:Longest Palindromic Substring 最长回文子串

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

随机推荐

  1. 计算机程序是怎么通过cpu,内存,硬盘运行起来的?

    虽然以前知道计算机里有CPU,内存,硬盘,显卡这么些东西,我还真不知道这些东西是怎么协作起来完成一段程序的,能写出程序却不懂程序,也不会向别人解释他们的关系,所以特意总结了一下,写的比较浅显,和我一样 ...

  2. 【JAVA】Class.getResource()与ClassLoader.getResource()的区别

    转载自:https://blog.csdn.net/qq_33591903/article/details/91444342 Class.getResource()与ClassLoader.getRe ...

  3. Python - 集合与元素之集合定义和基本操作方法

    集合(set) 定义:由不同元素组成的集合,集合中是一组无序排列可hash的值(不可变的值)例如数字.字符串.元组,可以作为字典的key 定义集合: # 定义集合 s = {1, 2, 3, 3, 3 ...

  4. id 工具: 查询用户所对应的UID 和GID 及GID所对应的用户组

    id 工具是用来查询用户信息,比如用户所归属的用户组,UID 和GID等:id 用法极为简单:我们举个例子说明一下: 语法格式: id  [参数]  [用户名] 至于有哪些参数,自己查一下 id -- ...

  5. springboot整合mybatis通用Mapper

    参考: https://blog.csdn.net/x18707731829/article/details/82814095 https://www.jianshu.com/p/6d2103451d ...

  6. macOS下安装openCV+Xcode配置

    macOS下安装openCV+Xcode配置打开终端 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Hom ...

  7. Java数据结构和算法(七)--AVL树

    在上篇博客中,学习了二分搜索树:Java数据结构和算法(六)--二叉树,但是二分搜索树本身存在一个问题: 如果现在插入的数据为1,2,3,4,5,6,这样有序的数据,或者是逆序 这种情况下的二分搜索树 ...

  8. 《2018年云上挖矿态势分析报告》发布,非Web类应用安全风险需重点关注

    近日,阿里云安全团队发布了<2018年云上挖矿分析报告>.该报告以阿里云2018年的攻防数据为基础,对恶意挖矿态势进行了分析,并为个人和企业提出了合理的安全防护建议. 报告指出,尽管加密货 ...

  9. Python中的一些模块用法

    python中os模块用法 python之模块之shutil模块 -------------------------------os模块-------------------------------- ...

  10. 手机前端开发调试利器-vConsole

    最近因为做抽奖页面,在android上可以使用手机连上电脑后用chrome浏览器chrome://inspect进行页面探测,但是ios中的页面就不能这样探测 在网上搜索后发现此插件,大大解决了问题 ...