classSolution { public: stringlongestPalindrome(string s){ if (s.size() < 2) return s; int n = s.size(), maxLen = 0, start = 0; for (int i=0; i<n-1; i++) { searchPalindrome(s, i, i, start, maxLen); searchPalindrome(s, i, i+1, start, maxLen); } return s.substr(start, maxLen); }
voidsearchPalindrome(string s, int left, int right, int &start, int &maxLen){ while (left>=0 && right<size() && s[left]==s[right]) { --left; ++right; } if (maxLen<right-left-1) { start = left +1; maxLen = right -left -1; } } };