5. 最长回文子串_字符串_中等

5. 最长回文子串_字符串_中等

 

 我的做法就是暴力,再加点剪枝来不超时。具体的思路是先从整体去识别这个字符串是否为回文串,如果不是再用连续的n-1个字符进行判断是否为回文,一旦是就不用继续判断其他的了,已经是最长的回文了,所以可以减少很多判断,效率比较高。

public String judge(String str) {
        int commonLength;
        String result = str.substring(0, 1);
        if (str.length() % 2 == 0) {
            commonLength = 0;
            int index = str.length() / 2;
            int left = index - 1;
            int right = index;
            while (true) {
                if (left < 0 || right >= str.length()) {
                    break;
                }
                if (str.charAt(left) == str.charAt(right)) {
                    commonLength += 2;
                    result = str.substring(left, right + 1);
                    left--;
                    right += 1;

                } else {
                    break;
                }
            }
        } else {
            commonLength = 1;
            int index = str.length() / 2;
            int left = index - 1;
            int right = index + 1;
            while (true) {
                if (left < 0 || right >= str.length()) {
                    break;
                }
                if (str.charAt(left) == str.charAt(right)) {
                    result = str.substring(left, right + 1);
                    commonLength += 2;
                    left--;
                    right += 1;
                } else {
                    break;
                }
            }
        }
        return result;
    }

    public String longestPalindrome(String s) {
        String commonStr = s.substring(0, 1);
        int commonLength = s.length();
        int resultLength = 1;
        while (true) {
            for (int i = 0; i < s.length(); i++) {
                if (commonLength + i <= s.length()) {
                    String result = judge(s.substring(i, i + commonLength));
                    if (result.length() > commonStr.length()) {
                        commonStr = result;
                        resultLength = commonStr.length();
                    }
                } else {
                    break;
                }
            }
            commonLength--;
            if (commonLength <= resultLength) break;
        }
        return commonStr;
    }

中心扩散法

5. 最长回文子串_字符串_中等

 

 思路大同小异,这个是从小扩大,我的是从大扩小,我的要高效些。

public String longestPalindrome1(String s) {

        if (s == null || s.length() == 0) {
            return "";
        }
        int strLen = s.length();
        int left = 0;
        int right = 0;
        int len = 1;
        int maxStart = 0;
        int maxLen = 0;

        for (int i = 0; i < strLen; i++) {
            left = i - 1;
            right = i + 1;
            while (left >= 0 && s.charAt(left) == s.charAt(i)) {
                len++;
                left--;
            }
            while (right < strLen && s.charAt(right) == s.charAt(i)) {
                len++;
                right++;
            }
            while (left >= 0 && right < strLen && s.charAt(right) == s.charAt(left)) {
                len = len + 2;
                left--;
                right++;
            }
            if (len > maxLen) {
                maxLen = len;
                maxStart = left;
            }
            len = 1;
        }
        return s.substring(maxStart + 1, maxStart + maxLen + 1);

    }

作者:reedfan
链接:https://leetcode-cn.com/problems/longest-palindromic-substring/solution/zhong-xin-kuo-san-fa-he-dong-tai-gui-hua-by-reedfa/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

动态规划。

记录已经计算过的区间的是否为回文。

public String longestPalindrome(String s) {
        if (s == null || s.length() < 2) {
            return s;
        }
        int strLen = s.length();
        int maxStart = 0;  //最长回文串的起点
        int maxEnd = 0;    //最长回文串的终点
        int maxLen = 1;  //最长回文串的长度

        boolean[][] dp = new boolean[strLen][strLen];

        for (int r = 1; r < strLen; r++) {
            for (int l = 0; l < r; l++) {
                if (s.charAt(l) == s.charAt(r) && (r - l <= 2 || dp[l + 1][r - 1])) {
                    dp[l][r] = true;
                    if (r - l + 1 > maxLen) {
                        maxLen = r - l + 1;
                        maxStart = l;
                        maxEnd = r;

                    }
                }

            }

        }
        return s.substring(maxStart, maxEnd + 1);

    }

作者:reedfan
链接:https://leetcode-cn.com/problems/longest-palindromic-substring/solution/zhong-xin-kuo-san-fa-he-dong-tai-gui-hua-by-reedfa/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

上一篇:用java模拟XN*2图灵机


下一篇:Java机试题:字符串加解密(大小写转化字符运算匹配)