leetcode-第五题最长回文子串

题目

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

示例 1:

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

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-palindromic-substring

动态规划

这个题或许是因为之前做过,一开始直接想到了是一个动态规划的题目。状态转移方程比较好得到,因为规律还是比较明显的。

s[i][j]代表i到j的子串,如果ci = cj且s[i+1][j-1]是回文(i+1 < j-1),那么s[i][j]也一定是回文。对于i==j的一定是回文,i=j-1的,只需要判断二者的字符是否相同即可。

代码如下:

class Solution {
    public String longestPalindrome(String s) {
        if (s.length() <= 1) return s;

        boolean [][]isPalindrome = new boolean[s.length()][s.length()];

        for (int i = 0; i < s.length(); i++) {
            isPalindrome[i][i] = true;
        }

        String result = s.substring(0, 1);
        int maxLength = 1;
        for (int width = 2; width <= s.length(); width++) {
            for (int i = 0; i < s.length() - width; i++) {
                boolean currentIsPal = (width == 2 || isPalindrome[i + 1][i + width - 2])
                        && s.charAt(i) == s.charAt(i + width - 1);
                if (currentIsPal) {
                    isPalindrome[i][i + width - 1] = true;
                    if (width > maxLength) {
                        maxLength = width;
                        result = s.substring(i, i + width);
                    }
                }
            }
        }
        return result;
    }
}

时间复杂度:O(n^{2})

空间复杂度:O(n^{2})

中心扩展

这个题还有一个巧妙的方法,因为最终的回文子串一定是有中心的。而且做长的回文子串也是从短的子串扩展判断来的,所以可以考虑从中心扩展去找最长的回文子串。而且这样的中心点个数是确定的,n为字符串s的长度,中心点个数为2n-1个。

每遍历到一个字符,可以以其为中心点扩散,也可以以其与右侧字符的中间位置作为中心点进行扩散。

代码如下:

class Solution {
    public String longestPalindrome(String s) {
        if (s.length() <= 1) return s;

        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);
    }

    public 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;
    }
}

时间复杂度:O(n^{2})

空间复杂度O(1)

暴力法

暴力法还是要说一下的,因为这是解题的第一方法。只有解决题目,才能考虑优化。

暴力法比较简单,就去判断所有子串是不是回文即可。

所有子串的可能性有O(n^{2})种,其实精确的说是\frac{n(n+1)}{2}种可能性(start为0有n种,start为1有n-1种……)。

然后每个子串判断是不是回文,时间复杂度为O(n),从两边往中间遍历比对即可。

manacher算法

这个暂时不看了

 

耗时:

番茄钟采用正向计时,耗时70分钟。

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,有三种方法可以解决LeetCode上的最长回文子串。 方法一是使用扩展心法优化,即从左向右遍历字符串找到连续相同字符组成的子串作为扩展心,然后从该心向左右扩展,找到最长回文子串。这个方法的时间复杂度为O(n²)。\[1\] 方法二是直接循环字符串,判断子串是否是回文子串,然后得到最长回文子串。这个方法的时间复杂度为O(n³),效率较低。\[2\] 方法三是双层for循环遍历所有子串可能,然后再对比是否反向和正向是一样的。这个方法的时间复杂度也为O(n³),效率较低。\[3\] 综上所述,方法一是解决LeetCode最长回文子串的最优解法。 #### 引用[.reference_title] - *1* [LeetCode_5_最长回文子串](https://blog.csdn.net/qq_38975553/article/details/109222153)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Leetcode-最长回文子串](https://blog.csdn.net/duffon_ze/article/details/86691293)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [LeetCode 第5最长回文子串(Python3解法)](https://blog.csdn.net/weixin_43490422/article/details/126479629)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值