题解 | #最长回文子串#
https://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param A string字符串
* @return int整型
*/
public int getLongestPalindrome (String s) {
// write code here
int n = s.length();
boolean[][] dp = new boolean[n][n];
for(int i = 0; i < n; i++ ) dp[i][i] = true;
int max = 0;
for(int i = n-1; i >= 0; i-- ){
for(int j = i; j < n; j++){
if(s.charAt(i) == s.charAt(j)){
dp[i][j] = j - i <= 2|| dp[i+1][j-1];
if(dp[i][j]) max = Math.max(max,j-i+1);
}
}
}
return max;
}
}
查看11道真题和解析