zoukankan      html  css  js  c++  java
  • Leetcode5.Longest Palindromic Substring最长回文字串

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

    示例 1:

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

    示例 2:

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

    暴力法:

    class Solution {
    public:
        string longestPalindrome(string s)
        {
            int len = s.size();
            int start = 0;
            int end = 0;
            int MAX = 1;
            for(int i = 0; i < len; i++)
            {
                for(int j = i + 1; j < len; j++)
                {
                    int flag = true;
                    for(int k = i; k < i + (j - i + 1) / 2; k++)
                    {
                        if(s[k] != s[j + i - k])
                        {
                            flag = false;
                            break;
                        }
                    }
                    if(flag)
                    {
                        if(j - i + 1 > MAX)
                        {
                            start = i;
                            end = j;
                            MAX = j - i + 1;
                        }
                    }
                }
            }
            string temp = "";
            for(int i = start; i <= end; i++)
            {
                temp += s[i];
            }
            return temp;
        }
    };

    动态规划:

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

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

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

    因此,

    P(i,j)=(P(i+1,j−1) and Si​==Sj​)

    基本示例如下:

    P(i,i)=true

    P(i,i+1)=(Si​==Si+1​)

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

    复杂度分析

    • 时间复杂度:O(n2), 这里给出我们的运行时间复杂度为 O(n2) 。
    • 空间复杂度:O(n2), 该方法使用 O(n2) 的空间来存储表。

    class Solution {
    public:
        string longestPalindrome(string s)
        {
            int size = s.size();
            int start = 0;
            int MAX = 0;
            vector<vector<bool> > dp(size, vector<bool>(size, false));
            //len代表从当前位置开始长度为len的字符串(不算上当前的字符串)
            //dp[i][j] == true 表示索引从i到j的子字符串是回文串
            for(int len = 0; len < size; len++)
            {
                for(int i = 0; i < size - len; i++)
                {
                    if(len == 0)
                    {
                        dp[i][i] = true;
                    }
                    else if(len == 1 && s[i] == s[i + len])
                    {
                        dp[i][i + len] = true;
                    }
                    else if(len > 1 && s[i] == s[i + len])
                    {
                        dp[i][i + len] = dp[i + 1][i + len - 1];
                    }
                    else
                    {
                        dp[i][i + len] = false;
                    }
    
                    if(dp[i][i + len] == true && len >= MAX)
                    {
                        MAX = len;
                        start = i;
                    }
                }
            }
            string res = "";
            for(int i = start; i <= start + MAX; i++)
                res += s[i];
            return res;
        }
    };
    
  • 相关阅读:
    SpringBoot监听Redis的Key过期事件
    because there was insufficient free space available after evicting expired cache entries
    springboot遇见netty 获取配置文件参数值为null
    spring cloud/spring boot同时支持http和https访问
    spring项目中引入AspectJ相关的Maven依赖【复制即可】
    解决spring中使用声明事务java.lang.NoClassDefFoundError: org/aspectj/util/PartialOrder$PartialComparable.
    maplestory【Lotus prequest】---2.2、request
    maplestory【Lotus prequest】---2.1、princess
    maplestory【Lotus prequest】---1.7、invincible
    maplestory【Lotus prequest】---1.6、orchid
  • 原文地址:https://www.cnblogs.com/lMonster81/p/10433903.html
Copyright © 2011-2022 走看看