题解 | #最长回文子串#
最长回文子串
http://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
动态规划,用前n-2的子串是不是回文子串判断n是否是回文子串
import java.util.*;
public class Solution {
public int getLongestPalindrome(String A, int n) {
// write code here
char[] list = A.toCharArray();
int[][] we = new int[list.length][list.length];
int max=1,begin,len;
int j;
for(int i=0;i<list.length;i++){
we[i][i] = 1;
}
for(int l=2;l<=list.length;l++){
for(int i =0;i<list.length;i++){
j = l+i-1;
if(j>=list.length){
break;
}
if(list[j] != list[i]){
we[i][j] = 0;
}else{
if(j-i<3){
we[i][j] =1;
}
else{
we[i][j] = we[i+1][j-1];
}
}
if(we[i][j] == 1 && j-i+1>max){
max = j-i+1;
}
}
}
return max;
}
}