一:题目 二:上码 class Solution {/**思路:1.确定dp数组2.确定dp数组的递推公式s[i] != s[j] 那么dp[i][j] 肯定为falses[i] == s[j] 那么的话 i - j <= 1 为true ans = max(ans,i-j + 1);i - j > 1的话 那么dp[i+1][j-1] == true的话那么dp[i][j] = trueans = max(ans,i-j+1);3.确定dp数组的初始化4.确定dp数组遍历顺序*/public String longestPalindrome(String s) {int len = s.length();int ans = 0;String str = "";boolean[][] dp = new boolean[len][len];//初始化为falsefor (int i = 0; i < len; i++) {for (int j = 0; j < len; j++) {dp[i][j] = false;}}//遍历顺序 从下到上 从左到右for (int i = len -1; i >= 0; i--) {for (int j = i; j < len; j++) {if (s.charAt(i) == s.charAt(j)) {if (Math.abs(i-j) <= 1) {dp[i][j] = true;//ans = Math.max(ans,Math.abs(i-j)+1);if (ans < Math.abs(i-j) + 1) {ans = Math.abs(i-j) + 1;str = s.substring(i,j+1);}}else if(dp[i+1][j-1]) {dp[i][j] = true;//ans = Math.max(ans,Math.abs(i-j)+1);if (ans < Math.abs(i-j) + 1) {ans = Math.abs(i-j) + 1;str = s.substring(i,j+1);}}}}}return str; } }