题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
动态规划,每次通过长度去递推 public class Solution { public int getLongestPalindrome(String A) { int res = 1; int n = A.length(); char[] a = A.toCharArray(); boolean[][] f = new boolean[n][n]; // 不能直接双重for循环,得通过长度去循环 for (int len = 0; len < n; len++) { for (int k = 0; k < n - len; k++) { int i = k; int j = k + len; if (a[i] == a[j]) { if (i == j || i + 1 == j) f[i][j] = true; else f[i][j] = f[i + 1][j - 1]; if (f[i][j]) res = Math.max(res, j - i + 1); } } } return res; } }