题解 | #最长回文子串#
最长回文子串
http://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
import java.util.*; public class Solution { public int getLongestPalindrome(String A, int n) { // write code here boolean[][] dp = new boolean[n][n]; for(int i=0;i<n;i++){ dp[i][i] = true; // 所有的单个字符都是回文串 } int maxLen = 0; for(int i=n-1;i>=0;i--){ for(int j=i+1;j<n;j++){ boolean flag = dp[i+1][j-1]; if(A.substring(i, j+1).length()<=3){ dp[i][j] = A.substring(i, i+1).equals(A.substring(j, j+1)); } else { dp[i][j] = A.substring(i, i+1).equals(A.substring(j, j+1)) && flag; } if(dp[i][j]){ maxLen = Math.max(maxLen, j-i+1); } } } return maxLen; } }