LeetCode笔记——5最长回文子串

题目:

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

示例 1:

输入: "babad"
输出: "bab"
注意: "aba"也是一个有效答案。

示例 2:

输入: "cbbd"
输出: "bb"

思路:看着都好复杂。。。。我只看懂有程序的一两个就算了。。。。

思路1:中心扩展算法

将字符串分为单核(奇数字符串)和双核(偶数字符串)的情况。对于奇数字符串,以该字符为中心,向两边扩展判断;对于偶数字符串,以该字符和下一个字符为中心扩展判断。使用了函数substring(i,j)截取一部分字符串.i表示起始索引位置(包括该字符);j表示结束索引(不包括)。

public String longestPalindrome(String s) {
    if (s == null || s.length() < 1) return "";
    int start = 0, end = 0;
    for (int i = 0; i < s.length(); i++) {
        int len1 = expandAroundCenter(s, i, i);  //针对单核字符串
        int len2 = expandAroundCenter(s, i, i + 1);  //针对双核字符串
        int len = Math.max(len1, len2);
        if (len > end - start) {
            start = i - (len - 1) / 2;
            end = i + len / 2;
        }
    }
    return s.substring(start, end + 1);
}

private int expandAroundCenter(String s, int left, int right) {
    int L = left, R = right;
    while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {
        L--;
        R++;
    }
    return R - L - 1;
}

思路二:动态规划(没看程序)

为了改进暴力法,我们首先观察如何避免在验证回文时进行不必要的重复计算。考虑 “ababa”\textrm{“ababa”}“ababa” 这个示例。如果我们已经知道 “bab”\textrm{“bab”}“bab” 是回文,那么很明显,“ababa”\textrm{“ababa”}“ababa” 一定是回文,因为它的左首字母和右尾字母是相同的。

我们给出 P(i,j)P(i,j)P(i,j) 的定义如下:

P(i,j)={true,false,如果子串Si…Sj是回文子串其它情况

因此,

P(i,j)=(P(i+1,j−1) and Si==Sj) P(i, j) = ( P(i+1, j-1) \text{ and } S_i == S_j ) P(i,j)=(P(i+1,j−1) and S​i​​==S​j​​)

基本示例如下:

P(i,i)=true P(i, i) = true P(i,i)=true

P(i,i+1)=(Si==Si+1) P(i, i+1) = ( S_i == S_{i+1} ) P(i,i+1)=(S​i​​==S​i+1​​)

这产生了一个直观的动态规划解法,我们首先初始化一字母和二字母的回文,然后找到所有三字母回文,并依此类推…

复杂度分析

  • 时间复杂度:O(n2)O(n^2)O(n​2​​), 这里给出我们的运行时间复杂度为 O(n2)O(n^2)O(n​2​​) 。

  • 空间复杂度:O(n2)O(n^2)O(n​2​​), 该方法使用 O(n2)O(n^2)O(n​2​​) 的空间来存储表。

思路三:

先把大神的代码贴在这里

public String longestPalindrome(String s) {
        List<Character> s_new = new ArrayList<>();
        for(int i = 0;i < s.length();i++){
            s_new.add('#');
            s_new.add(s.charAt(i));
        }
        s_new.add('#');
        List<Integer> Len = new ArrayList<>();
        String sub = "";//最长回文子串
        int sub_midd = 0;//表示在i之前所得到的Len数组中的最大值所在位置
        int sub_side = 0;//表示以sub_midd为中心的最长回文子串的最右端在S_new中的位置
        Len.add(1);
        for(int i = 1;i < s_new.size();i++){
            if(i < sub_side) {//i < sub_side时,在Len[j]和sub_side - i中取最小值,省去了j的判断
                int j = 2 * sub_midd - i;
                if(j >= 2 * sub_midd - sub_side &&  Len.get(j) <= sub_side - i){
                    Len.add(Len.get(j));
                }
                else
                    Len.add(sub_side - i + 1);
            }
            else//i >= sub_side时,从头开始匹配
                Len.add(1);
            while( (i - Len.get(i) >= 0 && i + Len.get(i) < s_new.size()) && (s_new.get(i - Len.get(i)) == s_new.get(i + Len.get(i))))
                Len.set(i,Len.get(i) + 1);//s_new[i]两端开始扩展匹配,直到匹配失败时停止
            if(Len.get(i) >= Len.get(sub_midd)){//匹配的新回文子串长度大于原有的长度
                sub_side = Len.get(i) + i - 1;
                sub_midd = i;
            }
        }
        sub = s.substring((2*sub_midd - sub_side)/2,sub_side /2);//在s中找到最长回文子串的位置
        return sub;

    }

猜你喜欢

转载自blog.csdn.net/chenxy132/article/details/82658756