leetcode 5最长回文子串

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

class Solution {
    public String longestPalindrome(String s) {
        if (s.length() < 1) {
            return s;
        }
        int[][] map = new int[s.length()][s.length()];
        for (int i = 0; i < s.length(); i++) {
            map[i][i] = 1;
        }
        char[] sChar = s.toCharArray();
        int maxLength = 1;
        int maxL = 0;
        int maxR = 0;
        for (int lenght = 2; lenght <= s.length(); lenght++) {
            for (int i = lenght - 1; i < s.length(); i++) {

                int k = i - lenght + 1;
                char char2 = sChar[k];
                char char1 = sChar[i];
                String string = s.substring(k, i + 1);
                if (sChar[k] == sChar[i] && (k + 1 > i - 1 || map[k + 1][i - 1] != 0)) {
                    if ((map[k][i] = map[k + 1][i - 1] + 2) > maxLength) {
                        maxL = k;
                        maxR = i;
                    }
                }
            }
        }
        return s.substring(maxL, maxR + 1);
    }
}

你可能感兴趣的:(算法研究)