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 ...
随机推荐
- dom 侧栏分享
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- [转载]关于AutoCAD.NET的辅助方法
转载自:http://www.cnblogs.com/milian/p/3315000.html 求中点坐标: /// <summary> /// 中点 /// </summary& ...
- datagridview里面有combox避免双击两次的写法
双击两次变成单击一次的写法: void dataGridView_CellEnter(object sender, DataGridViewCellEventArgs e) { //实现单击一次显示下 ...
- 图片转换成base64_encode的链接代码示例
<?php $file = "example.jpg"; $type = getimagesize( $file ); //取得图片的大小,类型等 $file_content ...
- 把我的Java项目部署到Linux系统
以前,还未毕业,凭借自己三脚猫的功夫,只会在Windows环境中使用tomcat容器把项目跑起来. 以前的操作是,利用Eclipse把项目导出成War包,放到tomcat的webApp文件夹中,鼠标点 ...
- MyArrayList——实现自己的ArrayList!
注:转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5965205.html ArrayList是我们常用的集合类之一,其实它的实现机制很简单,底层还是使用了一个 ...
- Android导入自定义的jar包时出现 E/AndroidRuntime(486): java.lang.NoClassDefFoundError错误
把自定义的jar包放在Android的工程的libs目录下,运行程序,会出现一下错误: 10-10 08:34:06.479: E/dalvikvm(486): Could not find clas ...
- web APi角色认证
http://www.cnblogs.com/youring2/archive/2013/03/09/2950992.html http://kb.cnblogs.com/page/107117/
- CFileDialog 、CFile 如何进行文件操作 [转]
如何进行文件操作 [1]显示对话框,取得文件名 CString FilePathName; CFileDialog dlg(TRUE);//TRUE为OPEN对话框,FALSE为SAVE AS对话框 ...
- 检索COM 类工厂中CLSID 为 {00024500-0000-0000-C000-000000000046}的组件时失败
在项目中将数据导出为Excel格式时出现“检索COM 类工厂中CLSID 为 {00024500-0000-0000-C000-000000000046}的组件时失败,原因是出现以下错误: 80070 ...