题目
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd"
Output: "bb"
思路
动态规划,若原回文子字符串的首尾相邻的字符相同,则可以组成新的回文子字符串,同时比较新的回文子字符串的长度与已有最长回文子字符串的长度,保留题目要求的最长回文子字符串,最后得到最长的回文子字符串。
代码
class Solution {
public:
string longestPalindrome(string s) {
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n));
string ans;
for(int l = 0; l < n; l++)
for(int i = 0; i + l < n; i++){
int j = i + l;
if(l == 0)
dp[i][j] = 1;
else if(l == 1)
dp[i][j] = s[i] == s[j];
else{
dp[i][j] = s[i] == s[j] && dp[i + 1][j - 1];
}
if(dp[i][j] && (l + 1) > ans.size())
ans = s.substr(i, l + 1);
}
return ans;
}
};