美文网首页
5. Longest Palindromic Substring

5. Longest Palindromic Substring

作者: becauseyou_90cd | 来源:发表于2018-07-27 10:00 被阅读0次

https://leetcode.com/problems/longest-palindromic-substring/description/

代码:
class Solution {
public String longestPalindrome(String s) {

    if(s == null && s.length() == 0) return null;
    int len = s.length();
    boolean[][] dp = new boolean[len][len];
    String res="";
    for (int i = len - 1 ; i >= 0; i--){
        for(int j = i; j < len; j++){
            if(s.charAt(i) == s.charAt(j) && (j - i < 3 || dp[i+1][j-1])){
                dp[i][j] = true;
                if(j - i + 1 > res.length())
                    res = s.substring(i,j+1);
            }
        }
    }
    return res;
}

}

相关文章

网友评论

      本文标题:5. Longest Palindromic Substring

      本文链接:https://www.haomeiwen.com/subject/pgdwmftx.html