【LeetCode】005. Longest Palindromic Substring

时间:2022-12-14 23:12:00

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example:

Input: "babad"

Output: "bab"

Note: "aba" is also a valid answer.

Example:

Input: "cbbd"

Output: "bb"

题解:

Solution 1

  暴力搜索,所有可能,注意到"bab"和"baab"两种类型的回文字符串即可。

 class Solution {
public:
string longestPalindrome(string s) {
int start = , len = ;
int n = s.size();
if (n < )
return s;
for (int i = ; i < n - ; ++i) {
rangeOfPalindrome(s, i, i + , start, len); // "baab"
rangeOfPalindrome(s, i, i, start, len); // "bab"
}
return s.substr(start, len);
} void rangeOfPalindrome(const string& s, int left, int right, int& start, int& len) {
int length = len;
int step = ;
while ((left - step >= ) && (right + step < s.size())) {
if (s[left - step] != s[right + step])
break;
++step;
}
length = * (step - ) + right - left + ;
if (length > len) {
len = length;
start = left - (step - );
}
} };

  代码简化:

 class Solution {
public:
string longestPalindrome(string s) {
string res;
int n = s.size(), idx = , len = ;
for (int i = ; i < n; ++i) {
search(s, i, i, idx, len);
search(s, i, i + , idx, len);
}
return s.substr(idx, len);
}
void search(const string& s, int i, int j, int& idx, int& len) {
while (i >= && j < s.size() && s[i] == s[j]) {
--i;
++j;
}
if (len < j - i - ) {
len = j - i - ;
idx = i + ;
}
}
};

Soluion 2

  DP问题。一个长度为 n(n>1) 的回文字符串S(s1, s2,...,sn),若将字符s0和sn+1分别放置在S的首尾,此时如果s0 == sn+1,那么新的字符串S'也一定是回文字符串。

  那么递归式为 dp[i][j] = 1                                            if i == j

           = s[i] == s[j]                            if i + 1 = j

= s[i] == s[j] && dp[i + 1][j - 1]   if i + 1 < j

 class Solution {
public:
string longestPalindrome(string s) {
int n = s.size();
if (n < )
return s;
int len = , start = ;
vector<vector<int>> dp(n, vector<int>(n, )); for (int i = ; i < n; ++i) {
for (int j = ; j <= i; ++j) {
dp[j][i] = (s[i] == s[j]) && (i - j <= || dp[j + ][i - ]);
if (dp[j][i] && len < i - j + ) {
len = i - j + ;
start = j;
}
}
} return s.substr(start, len);
}
};

  Manacher算法

Solution 3

【LeetCode】005. Longest Palindromic Substring的更多相关文章

  1. 【LeetCode】5&period; Longest Palindromic Substring 最长回文子串

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

  2. 【LeetCode】5&period; Longest Palindromic Substring 最大回文子串

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

  3. 【leetcode】5&period; Longest Palindromic Substring

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

  4. 【一天一道LeetCode】&num;5 Longest Palindromic Substring

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

  5. 【LeetCode】516&period; Longest Palindromic Subsequence 最长回文子序列

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...

  6. 【leetcode】516&period; Longest Palindromic Subsequence

    题目如下: 解题思路:很经典的动态规划题目,但是用python会超时,只好用C++了. 代码如下: class Solution { public: int longestPalindromeSubs ...

  7. 【SP1812】LCS2 - Longest Common Substring II

    [SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...

  8. 【SP1811】LCS - Longest Common Substring

    [SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...

  9. 【LeetCode】522&period; Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

随机推荐

  1. css背景图片定位练习(一)

    首先准备一张雪碧图,Like this 背景图片的定位方法有3种,比较常用的两种为 关键字:background-position: top left; (top/bottom/cennter/lef ...

  2. 局域网Internet的共享

    局域网接入Internet,之后,在服务器安装共享代理软件,可以使客户机通过代理软件接入Internet. 局域网接入Internet 而目前几乎所有的浏览器.下载软件.信件收发软件都支持代理服务器. ...

  3. 武汉科技大学ACM:1005&colon; 华科版C语言程序设计教程(第二版)例题5&period;8

    Problem Description 老师给小豪出了一道题目:给你两个整数x和n(-10<=x<=10,1<=n<=10),让你求出x^1+x^2+x^3+……+x^n的结果 ...

  4. LeetCode&lowbar;Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  5. Windows使用过程中的一些常见问题的解决方案

    Win8安装程序出现2502.2503错误解决方法 参见百度经验帖子:http://jingyan.baidu.com/article/a501d80cec07daec630f5e18.html

  6. Django performance

    Reference: https://impythonist.wordpress.com/2016/02/21/building-high-performance-django-systems/ Th ...

  7. linux centos7磁盘格式化挂载之parted

    parted /dev/xvde mklabel gpt //划分为gpt分区 mkpart logical //创建逻辑分区 ext4 //开始大小 537G //结束大小 quit blkid l ...

  8. Django 通过 mongoengine 连接 MongoDB 进而使用orm进行CRUD

    一. 在python脚本中, 我们通常可以使用pymongo模块实现与mongodb数据库的交互, 但是在使用Django框架进行定制开发的web server 项目中, 仍然使用pymongo模块的 ...

  9. geoserver 地图性能和缓存

    1.什么是GeoWebCache GeoWebCache是地图缓存软件公司成员开发的一个基于java的开源项目.和其他的缓存系统相似,它作为一个客户端和地图服务的代理.它可以单独部署,适用于任何基于W ...

  10. ibatis&lpar;sqlmap&rpar;中 &num;与&dollar;的使用区别

    在sqlmap文件中不使用“#VALUE#”来原样(参数对应什么类型,就当什么类型,比如拼凑的内容为string则自动加上了‘’)读取,而是$VALUE$方式来读取,即不加任何的东西,比如单引号啥的, ...