Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
用DP,d[i][j]表示字符串i到j是否为回文
public class Solution {
public String longestPalindrome(String s) {
int max = 0;
int len = s.length();
int maxL = 0;
int maxR = 0;
for(int i=0; i<len; i++){
int l=i;
int r=i+1;
while(l>=0 && r<len && s.charAt(l)==s.charAt(r)){
if(max < r-l+1){
max = r-l+1;
maxL = l;
maxR = r;
}
l--;
r++;
}
l = i;
r = i;
while(l>=0 && r<len && s.charAt(l)==s.charAt(r)){
if(max < r-l+1){
max = r-l+1;
maxL = l;
maxR = r;
}
l--;
r++;
}
}
return s.substring(maxL, maxR+1);
}
}