C++
typedef struct node{
int focus;
bool is_next_equal_focus = false;
int len = 0;
} node;
class Solution {
public:
string longestPalindrome(string s) {
std::vector<node> right;
for(int index = 0; index < s.length(); index++)
{
node *n = new node;
n->focus = index;
if((s.length()>(index+1))&&(s[index]==s[index+1]))
{
n->is_next_equal_focus = true;
for(int j = 0;((index - j) >= 0 && (index + 1 + j) < s.length()) && (s[index - j] == s[index + 1 + j]);j++)
n->len++;
right.push_back(*n);
}
else
{
n->is_next_equal_focus = false;
for(int j = 0;((index - j) >= 0 && (index + j) < s.length()) && (s[index - j] == s[index + j]);j++)
n->len++;
right.push_back(*n);
}
delete n;
}
int now = 0;
int i = 0;
for(int index = 0;index<right.size();index++)
{
if(right[index].len>now)
{
now = right[index].len;
i = index;
}
}
if(right[i].is_next_equal_focus)
{
return s.substr((right[i].focus-right[i].len+1),(right[i].len*2));
}
else
{
return s.substr((right[i].focus-right[i].len+1),(right[i].len)*2-1);
}
}
};评论 (0)


