• Java实现 LeetCode 5 最长回文子串


    5. 最长回文子串

    给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。

    示例 1:

    输入: “babad”
    输出: “bab”
    注意: “aba” 也是一个有效答案。
    示例 2:

    输入: “cbbd”
    输出: “bb”

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/longest-palindromic-substring
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    class Solution {
        public String longestPalindrome(String s) {
            if (s == null || s.length() == 0) {
                return "";
            }
    //         保存起始位置,测试了用数组似乎能比全局变量稍快一点
            int[] range = new int[2];
            char[] str = s.toCharArray();
            for (int i = 0; i < s.length(); i++) {
    //             把回文看成中间的部分全是同一字符,左右部分相对称
    //             找到下一个与当前字符不同的字符
                i = findLongest(str, i, range);
            }
            return s.substring(range[0], range[1] + 1);
        }
        
        public static int findLongest(char[] str, int low, int[] range) {
    //         查找中间部分		防止abbba
            int high = low;
            while (high < str.length - 1 && str[high + 1] == str[low]) {
                high++;
            }
    //         定位中间部分的最后一个字符
            int ans = high;
    //         从中间向左右扩散
            while (low > 0 && high < str.length - 1 && str[low - 1] == str[high + 1]) {
                low--;
                high++;
            }
    //         记录最大长度
            if (high - low > range[1] - range[0]) {
                range[0] = low;
                range[1] = high;
            }
            return ans;
        }
    }
    
  • 相关阅读:
    PHP中的NULL类型
    js中自定义事件,使用了jQuery
    chrome调试文章
    codeforces 633D
    hdu 1496 Equations
    poj 1286 Necklace of Beads
    poj 2154 Color
    poj 3270 Cow Sorting
    poj 1026 Cipher
    poj 2369 Permutations
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13075783.html
Copyright © 2020-2023  润新知