//字符串n,中心为n + n-1(空隙),从每一个中心往两边扫描,O(n^2)
改进:
public String longestPalindrome1(String s) { if (s == null || s.length() == 0) { return ""; } int len = s.length(); boolean[][] palin = new boolean[len][len]; String res = ""; int maxlen = 0; for (int i = len-1; i >= 0; i--) { for (int j = i; j < len; j++) { if (s.charAt(i) == s.charAt(j) && (j-i<=2 || palin[i+1][j-1])) { palin[i][j] = true; if (maxlen < j-i+1) { maxlen = j-i+1; res = s.substring(i,j+1); } } } } return res; }//动态规划,申请额外空间存储i~j是否回文
新闻热点
疑难解答