题解 | #二维数组中的查找#
最长回文子串
http://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
中心扩散法 Java版本
import java.util.*; public class Solution { public int getLongestPalindrome(String A, int n) { // write code here int length = A.length(); int maxLen = 1; // 遍历C位 for (int i = 0; i < length; i++) { int left = i - 1; int right = i + 1; int len = 1; // 判断左边是否与C位相同 while (left >= 0 && A.charAt(left) == A.charAt(i)) { len++; left--; } // 判断右边是否与C位相同 while (right < length && A.charAt(right) == A.charAt(i)) { len++; right++; } // 正常左右扩散 while (left >= 0 && right < length && A.charAt(left) == A.charAt(right)) { len += 2; left--; right++; } maxLen = Math.max(maxLen, len); } return maxLen; } }