LeetCode5:最长回文子串

参考链接:
🔗:【5. 最长回文子串 Longest Palindromic Substring【LeetCode 力扣官方题解】-哔哩哔哩】

具体代码

java 复制代码
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);
    }
    
}
相关推荐
文丑颜不良啊12 小时前
LeetCode2981~3000题目解析
java·leetcode
文丑颜不良啊19 小时前
LeetCode3001~3020题目解析
java·leetcode
Pniubi1 天前
力扣19 . 删除表的倒数第N个节点(栈)
算法·leetcode·职场和发展
文丑颜不良啊2 天前
LeetCode2761~2780题目解析
java·leetcode
文丑颜不良啊2 天前
LeetCode2841~2860题目解析
java·leetcode
文丑颜不良啊2 天前
LeetCode2921~2940题目解析
java·leetcode
文丑颜不良啊2 天前
LeetCode2901~2920题目解析
java·leetcode
文丑颜不良啊2 天前
LeetCode2861~2880题目解析
java·leetcode
文丑颜不良啊3 天前
LeetCode2821~2840题目解析
java·leetcode
Pniubi3 天前
力扣24. 两两交换链表中的节点(递归)
算法·leetcode·链表