Task17.最长回文子串

本文深入解析了LeetCode5题——最长回文子串的多种算法实现,包括暴力法和中心扩展法,详细展示了不同方法的时间复杂度及代码实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LeetCode5.最长回文子串

题目

在这里插入图片描述

代码

暴力法

时间复杂度:O(n3)

public class Solution 
{    
    public string LongestPalindrome(string s) 
    {
        string result = "";
        for (int i = 0; i < s.Length; i++)
        {
            for (int j = i; j < s.Length; j++)
            {
                int left = i, right = j;
                bool isPalindromic = true;
                while (left < right)
                {
                    if (s[left++] != s[right--])
                    {
                        isPalindromic = false;
                        break;
                    }
                }
                if (isPalindromic)
                {
                    int len = j - i + 1;
                    if (len > result.Length)
                        result = s.Substring(i, len);
                }
            }
        }
    }
}

在这里插入图片描述
中心法扩展法
时间复杂度:O(n3)

public class Solution 
{    
    public string LongestPalindrome(string s) 
    {
        if (s.Length < 2)            
            return s;        
        string result = "";        
        int n = s.Length;
        for (int i = 0; i < n; i++)
        {
            int left = i, right = i;
            while (left >= 0 && right < n)
            {
                if (s[left] != s[right])
                    break;
                left--;
                right++;
            }
            int len = right - left - 1;
            if (len > result.Length)
                result = s.Substring(left+1, len);
        }
        for (int i = 0; i < n; i++)
        {
            int left = i, right = i;
            while (left >= 0 && right < n)
            {
                if (s[left] != s[right])
                    break;
                left--;
                right++;
            }
            int len = right - left - 1;
            if (len > result.Length)
                result = s.Substring(left+1, len);
        }
        return result; 
    }
}

在这里插入图片描述
把两个for放在了一起

public class Solution 
{    
    public string LongestPalindrome(string s) 
    {
        if (s.Length < 2)            
            return s;        
        string result = "";        
        int n = s.Length;
        int left, right, len;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < 2; j++)
            {
                left = i;
                right = i + j;
                while (left >= 0 && right < n)
                {
                    if (s[left] != s[right])
                        break;
                    left--;
                    right++;
                }
                len = right - left - 1;
                if (len > result.Length)
                    result = s.Substring(left + 1, len);
            }
        return result;
    }
}

在这里插入图片描述

中心扩展法的另一种想法,即中心可以取在两个数中间。

时间复杂度:O(n2)

public class Solution 
{    
    public string LongestPalindrome(string s) 
    {
        if (s.Length < 2)
            return s;        
        string result = "";        
        int n = s.Length;        
        int end = 2 * n - 1;        
        for (int i = 0; i < end; i++)        
        {            
            double mid = i / 2.0;            
            int left = (int)(Math.Floor(mid));            
            int right = (int)(Math.Ceiling(mid));            
            while (left >= 0 && right < n)            
            {                
                if (s[left] != s[right])                     
                    break;                
                left--;                 
                right++;            
            }            
            int len = right - left - 1;            
            if (len > result.Length)                
                result = s.Substring(left + 1, len);        
        }        
        return result;        
    }
}

在这里插入图片描述

000

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西在路上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值