leetcode 5 最长回文子串

简介: leetcode 5 最长回文子串

最长回文子串

暴力法

class Solution {
public:
    bool cheak(string &s , int left ,int right)
    {
        for(int i = left , j = right ; i<j ; i++ , j--)
        {
            if(s[i] != s[j]) return false;
        }
        return true;
    }
    string longestPalindrome(string s) {
        if(s.size() <= 1) return s;
        int string_max = 0;
        string result;
        for(int i=0 ; i<s.size() ;i++)
        {
            for(int j=i ; j<s.size() ;j++)
            {
                if(s[j] == s[i] && cheak(s,i,j) == true && (j-i+1) > string_max)
                {
                    // cout<<i<<' '<<j<<endl;
                    string_max = j-i+1;
                    result.assign(s.begin()+i , s.begin()+j+1);
                } 
            }   
        }
        return result;
    }
};

动态规划

class Solution {
public:
    string longestPalindrome(string s) {
        if(s.size() <= 1) return s;
        vector<vector<bool>> dp(s.size() , vector<bool>(s.size() , false));
        int left = 0 , right = 0;
        for(int i=s.size()-1 ; i>=0 ; i--)
        {
            for(int j=i ; j<s.size() ;j++)
            {
                if(s[i] == s[j] && (j-i<=1 ||dp[i+1][j-1] == true) ) 
                {
                    dp[i][j] = true;
                    if( j-i > right -left) 
                    {
                        left = i;
                        right = j;
                    }
                }
            }
        }
        string result(s.begin()+left , s.begin()+right+1);
        return result;
    }
};

高频题(暴力法)

class Solution {
public:
    bool cheak(string &s , int left , int right)
    {
        for(int i=left , j=right ; i<j ; i++ ,j--)
        {
            if(s[i] != s[j]) return false;
        }
        return true;
    }
    string longestPalindrome(string s) {
        string result;
        for(int i=0 ; i<s.size() ;i++)
        {
            for(int j=i ; j<s.size() ;j++)
            {
                if(s[i] == s[j] && cheak(s,i,j) == true && (j-i+1) > result.size())
                {
                    string tmp(s.begin()+i , s.begin()+j+1);
                    result = tmp;
                }
            }
        }
        return result;
    }
};

高频题(动态规划)

class Solution {
public:
    string longestPalindrome(string s) {
        vector<vector<bool>> dp(s.size() , vector<bool>(s.size(),false));
        int left = 0 , right = 0;
        for(int i=s.size()-1 ; i>=0 ;i--)
        {
            for(int j=i ; j<s.size() ;j++)
            {
                if(s[i] == s[j] && ( j-i<=1 || dp[i+1][j-1] == true ))
                {
                    dp[i][j] = true;
                    if(j-i > right-left)
                    {
                        left = i;
                        right = j;
                    }
                }
            }
        }
        return string(s.begin()+left , s.begin()+right+1);
    }
};
相关文章
|
5月前
|
算法
LeetCode算法题---最长回文子串、N 字形变换(四)
LeetCode算法题---最长回文子串、N 字形变换(四)
37 0
|
2月前
|
Python
【Leetcode刷题Python】5. 最长回文子串
LeetCode 5题 "最长回文子串" 的Python解决方案,使用动态规划算法找出给定字符串中的最长回文子串。
34 3
|
2月前
|
算法
LeetCode第5题最长回文子串
该文章介绍了 LeetCode 第 5 题最长回文子串的解法,通过分析回文子串的特点,使用动态规划的思想,用二维数组记录字符串是否为回文串,从后往前遍历找子串,减少重复判断,最终找到最长回文子串,并总结了解题时通过举例推导找规律的思路。
LeetCode第5题最长回文子串
|
4月前
|
存储 算法 Java
【经典算法】LeetCode 5: 最长回文子串(Java/C/Python3实现含注释说明,Medium)
【经典算法】LeetCode 5: 最长回文子串(Java/C/Python3实现含注释说明,Medium)
35 2
|
5月前
leetcode-5:最长回文子串
leetcode-5:最长回文子串
43 0
|
5月前
leetcode代码记录(最长回文子串
leetcode代码记录(最长回文子串
39 2
|
4月前
dp表,哈希表,力扣5.最长回文子串力扣1745.分割回文串IV力扣132.分割回文串II优先级队列(堆)是什么下面是手动实现小根堆力扣1046.最后一块石头的重量
dp表,哈希表,力扣5.最长回文子串力扣1745.分割回文串IV力扣132.分割回文串II优先级队列(堆)是什么下面是手动实现小根堆力扣1046.最后一块石头的重量
|
5月前
|
索引
力扣---最长回文子串(动态规划)
力扣---最长回文子串(动态规划)
|
5月前
|
存储 算法 Go
LeetCode第五题: 最长回文子串
给定一个字符串 `s`​,找到 `s`​ 中最长的回文子串。你可以假设 `s`​ 的最大长度为 1000。
LeetCode第五题: 最长回文子串
【LeetCode 训练营 3,5】无重复字符的最长子串+最长回文子串
【LeetCode 训练营 3,5】无重复字符的最长子串+最长回文子串
81 1