【LeetCode】005. 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:
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的更多相关文章
- 【LeetCode】5. Longest Palindromic Substring 最长回文子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...
- 【LeetCode】5. Longest Palindromic Substring 最大回文子串
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- 【leetcode】5. Longest Palindromic Substring
题目描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- 【一天一道LeetCode】#5 Longest Palindromic Substring
一天一道LeetCode系列 (一)题目 Given a string S, find the longest palindromic substring in S. You may assume t ...
- 【LeetCode】516. Longest Palindromic Subsequence 最长回文子序列
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...
- 【leetcode】516. Longest Palindromic Subsequence
题目如下: 解题思路:很经典的动态规划题目,但是用python会超时,只好用C++了. 代码如下: class Solution { public: int longestPalindromeSubs ...
- 【SP1812】LCS2 - Longest Common Substring II
[SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...
- 【SP1811】LCS - Longest Common Substring
[SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
随机推荐
- mysql 7.5.8 服务无法启动 服务没有报告任何错误
你安装的mysql位置不是c盘的话应该出现此问题. 1.打开bin下的mysql_config.pl文件,查找以下几行,把目录改成你安装mysql的目录即可. my $ldata = 'C:/Prog ...
- Service Fusing
服务熔断也称服务隔离,来自于Michael Nygard 的<Release It>中的CircuitBreaker应用模式,Martin Fowler在博文CircuitBreaker中 ...
- ReactNative学习一
ReactNative 主要学习来源于RN官方文档https://reactnative.cn/docs/0.51/getting-started.html 不过除了这个RN官方文档,其他RN中文 ...
- Windos Server 2008 FTP 服务安装
安装服务:FTP 系统环境:Windos 2008 R2 x64 安装FTP服务 管理-->角色-->添加角色-->Web 服务器 IIS 测试
- SQL性能调优策略
1.建立索引 2.避免全表扫描 避免使用is null, is not null,这样写会放弃该字段的索引. 如果会出现这种情况,尽量在设计表的时候设置默认值 比较操作符中!= <>等避免 ...
- 修改jpivot源码实现分页
使用jpivot过程中,如果查询到的结果行数超过一个阈值,后面的显示就会丢失,这时需要分页显示. 假设应用中组装的MDX语句已经含有NON EMPTY,把空行直接过滤掉了. 这时需要修改的jpivot ...
- JavaScript -- 练习,Dom 获取节点
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- php环境之Wampserver端口修改
WampServer是一款由法国人开发的Apache Web服务器.PHP解释器以及MySQL数据库的整合软件包.免去了开发人员将时间花费在繁琐的配置环境过程,从而腾出更多精力去做开发.WampSer ...
- UOJ283 直径拆除鸡
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 10.0.4_CentOS_120g_for_Qt5.3.2
对应 VMware Workstation 版本为:“10.0.4 build-2249910”