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.
题意是找出字符串S中最长回文子串,S最长为1000,保证有唯一解
class Solution {
public:
string longestPalindrome(string s) {
string news = "";
for(int i=0; i<s.length(); i++){
news += "#";
news += s[i];
}
news += "#";
int maxStart = -1; //最大回文起始位置
int maxEnd = -1; //最大回文结束位置
int maxRadius = 0; //最大回文半径
int maxCenter = 1; //最大回文中心位置
int cur = 1; // 回文中心
while(cur < news.length()-1){
int radius = 0;
while(cur-radius>=0 && cur+radius<=news.length()-1 && news[cur-radius]==news[cur+radius]){
radius++;
}
radius--;
if(radius>maxRadius){
maxCenter = cur;
maxRadius = radius;
maxStart = cur-radius;
maxEnd = cur+radius;
}
cur++;
}
// maxStart和maxEnd肯定是指向#的,因此需要转换成原文中对应字符的位置
maxStart = maxStart/2;
maxEnd = maxEnd/2-1;
return s.substr(maxStart, (maxEnd-maxStart+1));
}
};
/*
题意是找出字符串S中最长回文子串,S最长为1000,保证有唯一解
思路:
DP
创建回文判别矩阵使用,假设A[i][j]表示S[i~j]的子串是否为回文串
转换方程A[i][j]=A[i+1][j-1] && S[i]==S[j]
*/
class Solution {
public:
string longestPalindrome(string s) {
int len = s.length();
if(len==0)return "";
vector<vector<bool> > isPal(len, vector<bool>(len, false));
int maxSubStrLen=0;
int maxSubStrStart=0;
//初始化斜对角线,即isPal[i][i], 单个字符肯定是回文
for(int i=0; i<len; i++){
isPal[i][i]=true;
if(1>maxSubStrLen){maxSubStrLen=1; maxSubStrStart=i;}
}
//初始化相邻字符构成的子串是否是回文
for(int i=0; i<len-1; i++){
isPal[i][i+1]=s[i]==s[i+1];
if(isPal[i][i+1] && 2>maxSubStrLen){maxSubStrLen=2; maxSubStrStart=i;}
}
//初始化i<j的区域
for(int i=len-3; i>=0; i--){
for(int j=len-1; j>=i+2; j--){
isPal[i][j]=isPal[i+1][j-1]&&s[i]==s[j];
if(isPal[i][j] && j-i+1>maxSubStrLen){maxSubStrLen=j-i+1; maxSubStrStart=i; }
}
}
return s.substr(maxSubStrStart, maxSubStrLen);
}
};LeetCode-005 Longest Palindromic Substrin,布布扣,bubuko.com
LeetCode-005 Longest Palindromic Substrin
原文地址:http://blog.csdn.net/harryhuang1990/article/details/25735085