LeetCode 5 最大回文子串

最大回文子串问题  暴力法肯定超时  请参考百度上的搜索结果 这里只是简单提一下和心思想
加什么符号%@#&……的不说了随意就好 设置数组其他文章里也有(这里为len[])
这里说三个主要变量
例子 12321**************(后面的元素暂时没有遍历到)
max 当前已经搜索到的最大字串的半径(max=3)
pos 最大字串对称点(2,0开始)
I   当前遍历到的元素序号
核心思想为三个判断
1. I<max且max-I>len[2*pos-i](找到与i关于pos对称位置的元素)。这里说的是根据回文字串特性,如果当前的元素s[I]在一个更大的回文字串中,且距离边界的距离小雨i关于pos对称点的距离,说明以s[I]为中心的回文字串完全包括在这个更大的字串中,且有一个对称的“兄弟”。这时候len[i]=len[2-pos-i]
2. I<max且max-I<=len[2*pos-i] 这情况说明以I为中心的回文串可能会超过最大边界max,这时候就需要进行中心扩展匹配, 算法大家都会。
3. I>max 没说的 这时候已经遍历到未知区域了 只能去匹配。
 c# code
public class Solution
{
    public string LongestPalindrome(string str)
    {
        string s = Init(str);
        //Console.WriteLine(s);
        int[] len = new int[s.Length];
        int max = 0, pos = 0;
        for (int i = 0; i < len.Length; i++)
        {
            if (i < max)
            {
                if (len[2 * pos - i] < max - i)
                {
                    len[i] = len[2 * pos - i];
                }
                else
                {
                    int a = Extend(i, s);
                    if (a > max)
                    {
                        max = a;
                        pos = i;
                    }
                }
            }
            else
            {
                int a = Extend(i, s);
                if (a > max)
                {
                    max = a;
                    pos = i;
                }
            }
        }
        string result = "";
        for (int i = pos - max + 1; i < pos + max - 1; i++)
        {
            result += s[i];
        }
        return result.Replace("#","");
    }
    static string Init(string s)
    {
        char[] str = new char[2 * s.Length + 1];
        for (int i = 0; i < s.Length; i++)
        {
            str[i * 2 + 1] = s[i];
            str[2 * i] = '#';
            str[str.Length - 1] = '#';
        }
        return new string(str);
    }
    static int Extend(int i,string s)
    {
        int a = 0;
        while (i - a >= 0 && i + a < s.Length && s[i + a] == s[i - a])
        {
            a++;
        }
        return a;
    }
}

  • 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、付费专栏及课程。

余额充值