LeetCode5 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, and there exists one unique longest palindromic substring. (Medium)
分析:
采用动态规划,dp[i][j]表示从s[i]到s[j]的字串是否回文,然后按照字串长度进行遍历,如果s[i] == s[j] && dp[i + 1][j - 1] == 1,则dp[i][j] = 1
并更新起始位置和最大长度。
初始化除了要将dp[i][i] i=0,1,2...n-1初始化外,还需要初始化dp[i][i+1]
最终得到维护好的最大字串起始位置和长度,返回该字串。
代码:
class Solution {
public:
string longestPalindrome(string s) {
int dp[][] = {};
int start = , length = ;
for (int i = ; i < s.size(); ++i) {
dp[i][i] = ;
}
for (int i = ; i < s.size() - ; ++i) {
if (s[i] == s[i + ]) {
dp[i][i + ] = ;
start = i;
length = ;
}
}
for (int k = ; k < s.size(); ++k) {
for (int i = ; i < s.size() - k; ++i) {
if (s[i] == s[i + k] && dp[i + ][i + k - ] == ) {
dp[i][i + k] = ;
start = i;
length = k + ;
}
}
}
return s.substr(start,length);
}
};
时间复杂度 O(n^2), 空间复杂度 O(n^2);
解法2:
考虑不采用dp数组,节省空间复杂度为O(1);
对每个每个节点作为初始位置,利用helper函数,开始向外扩展,如果满足s[left] == s[right], left--; right++继续判定;
直到不满足条件位置或边界,并更新最大长度和初始位置。
注意拓展节点时除了拓展单一节点情况,还需要拓展两个节点开始的情况 (与dp的初始化条件类似);
代码:
class Solution {
private:
int helper(const string& s, int left, int right) {
int length = ;
while (left >= && right < s.size() && s[left] == s[right]) {
length = right - left + ;
left--;
right++;
}
return length;
}
public:
string longestPalindrome(string s) {
int start = , length = ;
for (int i = ; i < s.size() ; ++i) {
int l1 = helper(s, i, i);
if (l1 > length) {
length = l1;
start = i - (length / );
}
}
for (int i = ; i < s.size() - ; ++i) {
int l2 = helper(s, i, i+);
if (l2 > length) {
length = l2;
start = i - (length - ) / ;
}
}
return s.substr(start, length);
}
};
LeetCode5 Longest Palindromic Substring的更多相关文章
- leetcode--5. Longest Palindromic Substring
题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- LeetCode-5:Longest Palindromic Substring(最长回文子字符串)
描述:给一个字符串s,查找它的最长的回文子串.s的长度不超过1000. Input: "babad" Output: "bab" Note: "aba ...
- LeetCode5. Longest Palindromic Substring 最长回文子串 4种方法
题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题意很简单,就是求一个字符串得最长子串,这里的子串指连续的. 本文给 ...
- Leetcode5.Longest Palindromic Substring最长回文字串
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &quo ...
- Leetcode5:Longest Palindromic Substring@Python
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- LeetCode5:Longest Palindromic Substring
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- [Swift]LeetCode5. 最长回文子串 | Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
随机推荐
- 第三百二十五天 how can I 坚持
任何事情都是相对的,以后禁止专牛角尖. 今天在家堕落了一天,说好的把天气应用,照葫芦画瓢弄好,结果什么也没弄. 和你 有个毛线关系啊,哈哈,太逗了. 准备睡觉,一切随缘,反正想也没什么用,自己也搞不懂 ...
- 转】MyEclipse使用总结——MyEclipse去除网上复制下来的来代码带有的行号
原博文出自于: http://www.cnblogs.com/xdp-gacl/p/3544208.html 感谢! 一.正则表达式去除代码行号 作为开发人员,我们经常从网上复制一些代码,有些时候复制 ...
- HDU2033 人见人爱A+B 分类: ACM 2015-06-21 23:05 13人阅读 评论(0) 收藏
人见人爱A+B Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- POJ1837 Balance(DP)
POJ1837http://poj.org/problem?id=1837 题目大意就是说有一个称上有C个挂钩,告诉你每个挂钩的位置,现在有G个重物,求是之平衡的方法数. 转化一下:DP[i][j]表 ...
- 转载sublime text注册码
直接输入注册码就可以了 ----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E-855605 813A03DD 5E4AD9E6 ...
- hdoj 5392 Infoplane in Tina Town
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5392 #include<stdio.h> #include<cstring> ...
- VM VirtualBox 上安装 CentOs6.4(详细)
在网上下载:CentOS-6.4-i386-bin-DVD1.iso镜像. 这是我在VBox上安装CentOs6.4的过程: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12 ...
- 当WEB站点应用程序池标识为ApplicationPoolIdentity,出现运行错误时的解决方法
对于数据库文件加Authenticated Users用户,并授予完全权限.
- Kafka架构设计:分布式发布订阅消息系统
[http://www.oschina.net/translate/kafka-design](较长:很详细的讲解) [我们为什么要搭建该系统]用作LinkedIn的活动流(activity stre ...
- SOA服务开发小计
http://item.jd.com/11181846.html#comment SOA面向服务架构——SOA的概念 http://www.cnblogs.com/leslies2/archive/2 ...