5. Longest Palindromic Substring
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) {
string res;
for(int i=0;i<s.size();i++)
{
for(int j=i,k=i;j>=0&&k<s.size()&&s[j]==s[k];j--,k++)
if(res.size()<k-j+1)
res=s.substr(j,k-j+1);
for(int j=i,k=i+1;j>=0&&k<s.size()&&s[j]==s[k];j--,k++)
if(res.size()<k-j+1)
res=s.substr(j,k-j+1);
}
return res;
}
};