题解 | #螺旋矩阵#
最长回文子串
http://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
中心扩散法,从每一个长度为1和2的子串出发,找到所有可能情况
时间O(n2),空间O(1)
class Solution {
public:
int expand(string &s, int left, int right){
while(left >= 0 && right < s.size() && s[left] == s[right]){
left--;
right++;
}
return right - left - 1;
}
int getLongestPalindrome(string A, int n) {
// write code here
int res = 1;
for(int i = 0; i < n; i++){
int len1 = expand(A, i, i);
int len2 = expand(A, i, i+1);
res = max(res, max(len1, len2));
}
return res;
}
};