//好晚了自己竟然没写出来
//想起这道题是大一C语言期末考试的一道题莫名悲伤
//唉
//感谢https://www.cnblogs.com/love-yh/p/7071871.html
class Solution {
public:
string longestPalindrome(string s)
{
string res="";
int len=s.size();
if(len==1) return s;
int maxLen=0,curLen=0,sbegin;
for(int i=0;i<len;i++)
{
//奇数
int left=i-1,right=i+1;
while(left>=0&&right<len&&s[left]==s[right])
{
curLen=right-left;
if(curLen>maxLen)
{
maxLen=curLen;
sbegin=left;
}
left--,right++;
}
//偶数
left=i,right=i+1;
while(left>=0&&right<len&&s[left]==s[right])
{
curLen=right-left;
if(curLen>maxLen)
{
maxLen=curLen;
sbegin=left;
}
left--,right++;
}
}
res=s.substr(sbegin,maxLen+1); //substring()为前闭后开
return res;
}
};