从字符串的长度开始寻找回文子串:(跟这道题有点相像的是最长重复子串)
import java.util.*; public class Solution { public int getLongestPalindrome(String A, int n) { // write code here StringBuffer buffer = new StringBuffer(A); String B = buffer.reverse().toString(); int len = A.length(); char[] a = A.toCharArray(); char[] b = B.toCharArray(); for (int i = len; i > 1; i--) { for (int j = 0; j <= len - i; j++) { boolean flag = true; for (int k = 0; k < i; k++) { if (a[j + k] != b[len - j - i + k]) { // 找对坐标关系,一切的一切都会很好办 flag = false; break; } } if (flag) { return i; } } } return 1; } }