public String longestPalindrome(String s) { if(s == null||s.length()==0){ return s; } String res = ""; int max=0; boolean[][] dp = new boolean[s.length()][s.length()]; //res=s.substring(0, 1); for(int j = 0;j < s.length();j++){ for(int i=0;i<=j;i++){ dp[i][j] = (s.charAt(i) == s.charAt(j)) && (((j - i + 1) <= 2) || dp[i+1][j-1]); if (dp[i][j] && (j-i+1)>max){ max = j-i+1; res = s.substring(i, j+1); } } } return res; } }
原文地址:https://www.cnblogs.com/q-1993/p/10765668.html
时间: 2024-10-10 09:33:22