题解 | #最长回文子串#
最长回文子串
http://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
import java.util.*; public class Solution { public int getLongestPalindrome(String A, int n) { // write code here int strlen = A.length(); int len = 1; int maxlen = 0; int left = 0; int right = 0; for(int i =0 ; i < strlen; ++i) { left = i - 1; right = i + 1; while(left >= 0 && A.charAt(i) == A.charAt(left)) { len++; left--; } while(right < strlen && A.charAt(right) == A.charAt(i)) { len++; right++; } while(left >= 0 && right < strlen && A.charAt(left) == A.charAt(right)){ len += 2; left--; right++; } if(maxlen < len) { maxlen = len; } len = 1; } return maxlen; } }