题目:
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) {
for (int i = s.size(); i > 0; i--)
{
for (int j = 0; j <= s.size() - i; j++)
{
string sub = s.substr(j, i);
bool flag = true;
for (int x = 0, y = sub.size() - 1; x <= y; x++, y--)
{
if (sub[x] != sub[y])
{
flag = false;
break;
}
}
if (flag)
return sub;
}
}
return s;
}
};