Commit 5b21604f authored by yaozc's avatar yaozc
Browse files

keep going

parent 682b82cf
......@@ -1558,6 +1558,52 @@ public:
Longest Palindromic Substring
// DP 还有bug
class Solution {
public:
string longestPalindrome(string s) {
if (s.empty()) return "";
vector<vector<bool>> table(s.size(), vector<bool>(s.size(), false));
for(int i=0; i<s.size(); i++){
table[i][i] = true;
}
int longest = 1;
string longest_pali(1, s[0]);
for(int i=0; i<s.size()-1; i++){
if(s[i] == s[i+1]) {table[i][i+1] = true; longest_pali = s.substr(i, 2);}
}
for(int size=2; size<s.size(); size++){
for(int start=0; start<s.size()-size; start++){
int end = start + size;
if(s[start] == s[end] && table[start+1][end-1]){
if(end-start+1 > longest) longest_pali = s.substr(start,end-start+1);
}
}
}
return longest_pali;
}
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment