1. 程式人生 > >leetcode第五題—最長迴文字串

leetcode第五題—最長迴文字串

string longestPalindrome(string &s)
{
	int n=s.size();
	if(n==0)
		return " ";
	string longest=s.substr(0,1);


	for(int i=0;i<n-1;i++)
	{
		//center is the character,the length is must be odd
		//like aba  cabac
		string p1=expandAroundCenter(s,i,i,n);
		if(p1.size()>longest.size())
			longest=p1;

		//center is the interval,the length is must be  even
		//like  bb  abba  cabbac
		string p2=expandAroundCenter(s,i,i+1,n);
		if(p2.size()>longest.size())
			longest=p2;
	}

	return longest;
}