思路
s[3][7] 如果是回文串,那么s[4][6]一定也是,然后在需要判断两个顶点
需要考虑两个特殊情况
- abcd a、b、c、d都可以
- aa
代码
public String longestPalindrome(String s) {
int[][] map = new int[1001][1001];
int[] result = new int[]{0,0};
for (int i = 0; i < s.length(); i++) {
map[i][i] = 1;
}
for (int i = 0; i+1 < s.length(); i++) {
if (s.charAt(i) == s.charAt(i+1)){
result[0] = i;
result[1] = i+1;
map[i][i+1] = 1;
}
}
for (int i = 2 ; i < s.length(); i++){
for (int j = 0; j+i < s.length(); j++) {
if (map[j+1][j+i-1] == 1 && s.charAt(j) == s.charAt(j+i)){
result[0] = j;
result[1] = j+i;
map[j][j+i] = 1;
}
}
}
return s.substring(result[0], result[1]+1);
}