LintCode 算法(中等)最长回文子串

LintCode 算法(中等)最长回文子串

题目
给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串。

样例
给出字符串 “abcdzdcab”,它的最长回文子串为 “cdzdc”。

挑战
O(n2) 时间复杂度的算法是可以接受的,如果你能用 O(n) 的算法那自然更好。

思路
由于能力有限O(n)算法实在是搞不定,而遍历所有字符串中子串算法时间复杂度度达到了O(n3),不符合性能要求,所以换个思路选择使用中心扩展进行判断。简单点说把字符串中每个子串当成是最长回文子串的中心,那么在其左右两则字符应当满足回文规则。如aba,abbac。
这里需要注意的是最长回文子串可能是偶数长度(abbac最长子串为abba)或奇数长度(aba最长子串为aba),在进行判断时逻辑有稍许不同。
奇数子串:只需把字符串中每个子串当成是最长回文子串的中心,向两边遍历是否满足回文规则即可
偶数子串:需将当前索引位index以及index+1字符合并看成最长回文子串的中心,向两边遍历是否满足回文规则即可

在字符串中可能同时出现满足回文条件的奇数子串,偶数子串。所以需要回文长度需要进行判断。

public class Solution {
    /**
     * @param s: input string
     * @return: the longest palindromic substring
     */
    public String longestPalindrome(String s) {
        //中心扩展算法
        String m="";
        int index=0;
        if(s.length()==1) return s;
          //index为中心点像两边扩展
            while (index<s.length())
            {
                int front=index-1;
                int last=index+1;
                if(front<0 || last>s.length())
                {
                    index++;
                    continue;
                }
                for(;front>=0 && last<s.length();front--,last++)
                {
                   char left= s.charAt(front);
                    char rghit= s.charAt(last);

                    if(left!=rghit )
                    {
                        break;
                    }
                }

                    int beginIndex=front+1;
                    int endIndex=last;
                    String temp=s.substring(beginIndex,endIndex);
                    m=temp.length()>m.length()?temp:m;
                index++;
            }


         index=0;
        //index,index+1为中心点像两边扩展
        while (index<s.length())
        {
            int front=index;
            int last=index+1;
            if(front<0 || last>s.length())
            {
                index++;
                continue;
            }
            for(;front>=0 && last<s.length();front--,last++)
            {
                char left= s.charAt(front);
                char rghit= s.charAt(last);

                if(left!=rghit )
                {
                    break;
                }
            }

            int beginIndex=front+1;
            int endIndex=last;
            String temp=s.substring(beginIndex,endIndex);
            m=temp.length()>m.length()?temp:m;
            index++;
        }


        return m;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用中的代码是一个使用暴力解法求解最长回文子串的示例。该代码通过截取字符串的所有子串,并判断这些子串中哪些是回文的,最后返回回文子串中最长的。这个算法的时间复杂度较高。引用中的代码是使用边界扩散法求解最长回文子串的示例。该算法通过从中心向两边扩展的方式,依次判断是否为回文子串,并记录最长回文子串的起止位置。这个算法的时间复杂度较低。引用中的代码也是使用边界扩散法求解最长回文子串的示例。该代码通过遍历字符串,以每个字符为中心,向两边扩展,判断回文子串的长度,并记录最长回文子串的起止位置。最后返回最长回文子串。这个算法的时间复杂度较低。根据引用和引用中的代码,可以整合出一个完整的Python代码来求解最长回文子串: class Solution: def isPalindrome(self, s, start, end): while start < end: if s[start != s[end]: return False start += 1 end -= 1 return True def longestPalindrome(self, s): max_len = 0 start = 0 end = 0 for i in range(len(s)): j = i + 1 while j < len(s): if self.isPalindrome(s, i, j): if (j - i + 1) > max_len: max_len = j - i + 1 start = i end = j + 1 j += 1 return s[start:end] s = "ac" S = Solution() result = S.longestPalindrome(s) print(result) 这段代码使用边界扩散法来求解最长回文子串。首先定义了一个isPalindrome函数来判断一个子串是否为回文子串。然后,在longestPalindrome函数中,通过两层循环遍历字符串,以每个字符为中心,向两边扩展,判断回文子串的长度,并记录最长回文子串的起止位置。最后返回最长回文子串。对于输入字符串"ac",输出结果为"a"。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Python最长回文子串](https://blog.csdn.net/weixin_42698464/article/details/121389797)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [5.最长回文子串 && 647.回文子串 (python)](https://blog.csdn.net/cqjnovo/article/details/124318575)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Python:实现最长回文子串算法(附完整源码)](https://blog.csdn.net/it_xiangqiang/article/details/128828664)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值