参考链接:
🔗:【5. 最长回文子串 Longest Palindromic Substring【LeetCode 力扣官方题解】-哔哩哔哩】
具体代码
class Solution {public String longestPalindrome(String s) {int i=0,j,k=0,n=s.length(),max_len=1,begin=0;char[] ch = s.toCharArray();boolean[][] dp=new boolean[n][n];if(s==null||n==0){return null;}if(n==1){return s;}for(j=1;j<n;++j){for (i=0;i<j;++i){if(ch[i]!=ch[j]){dp[i][j]=false;}else{if(j-i<=2){dp[i][j]=true;}else{dp[i][j]=dp[i+1][j-1];}if(dp[i][j]==true&&j-i+1>max_len){max_len=j-i+1;begin=i;}}}}return s.substring(begin,begin+max_len);}}