题解 | #最长回文子串#

最长回文子串

http://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af

import java.util.*;

public class Solution {
    public int getLongestPalindrome(String A, int n) {
        // write code here
        boolean[][] dp = new boolean[n][n];
        for(int i=0;i<n;i++){
            dp[i][i] = true; // 所有的单个字符都是回文串
        }
        int maxLen = 0;
        for(int i=n-1;i>=0;i--){
            for(int j=i+1;j<n;j++){
                boolean flag = dp[i+1][j-1];
                if(A.substring(i, j+1).length()<=3){
                    dp[i][j] = A.substring(i, i+1).equals(A.substring(j, j+1));
                } else {
                    dp[i][j] = A.substring(i, i+1).equals(A.substring(j, j+1)) && flag;
                }
                if(dp[i][j]){
                    maxLen = Math.max(maxLen, j-i+1);
                }

            }
        }
        return maxLen;
    }
}
全部评论

相关推荐

见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
_mos_:我以为手抄报简历就已经很顶了,没想到还有表格简历
点赞 评论 收藏
分享
07-02 18:09
门头沟学院 Java
苍穹外卖和谷粒商城这俩是不是烂大街了,还能做吗?
想去重庆的鸽子在吐槽:你不如把这俩做完自己搞明白再优化点再来问 何必贩卖焦虑
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务