LeetCode-5. Longest Palindromic Substring(M)

解法

比较经典的问题,寻找最长回文子串。Leetcode里提供了多种解法。我采用最直观的解法:中心扩展法。

思路是每次以当前元素为中心向两边扩展,直到遇到不同元素,此时找到一个子串。有两点需要注意的地方:

1)空串和单字符都是回文,直接返回即可。

2)偶数回文和奇数回文的情况。例如:abace是aba的奇数串,以b为中心向两周扩展即可。但是abbawe是偶数串abba,这样需要以bb为指针向两周同时往外扩展。

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example 1:

Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.

Example 2:

Input: "cbbd"
Output: "bb"
class Solution:
    def longestPalindrome(self, s: str) -> str:
        # 空串和长度为1的字串本身就是回文子串
        if len(s)<=1:
            return s

        temp = ""
        for i in range(len(s)-1): #i为当前字符,遍历整个字串
            # left  i  right   i means current char
            left = right = i   #奇数回文串情况
            while(left>=0) and (right<len(s)) and (s[left] == s[right]):
                #奇数回文串,所以k j指针指向i两边的元素
                left,right = left-1, right+1

            temp1 = s[left+1:right]
            # if else简洁写法,类似3目运算符
            temp = temp1 if len(temp1) > len(temp) else temp

            if(s[i] == s[i+1]):
                left,right = i,i+1
                # 重点在while的用法,不断的循环,直到找到第一个不满足条件的位置跳出
                while(left>=0) and (right < len(s)) and (s[left] == s[right]):
                    left,right = left-1, right+1
                #因为这行紧挨着while,说明刚好不满足条件,          #满足条件left = left+1, right = right + 1, 因为Python是左闭右开的区间,所以right不用-1
                temp1 = s[left+1:right]
                temp = temp1 if len(temp1) > len(temp) else temp

        return temp

原文地址:https://www.cnblogs.com/focus-z/p/12311958.html

时间: 2024-11-13 19:56:05

LeetCode-5. Longest Palindromic Substring(M)的相关文章

LeetCode: Longest Palindromic Substring(Medium)

原题链接:https://leetcode.com/problems/longest-palindromic-substring/description/ 1. 题目要求:找出字符串中的最大回文子串 2. 注意:要考虑回文子串中的字符个数是奇数还是偶数!!! 例如,"aabaa"是一个奇数个字符的回文字符串,他的中心只有一个字符"b". "aabbaa"是一个偶数个字符的回文字符串,他的中心却有两个相同字符"bb" 3. 思路

LeetCode 5 Longest Palindromic Substring(最长子序列)

题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 动态规划,类似于l

LeetCode 5 Longest Palindromic Substring(最大回文子字符串)

翻译 给定一个字符串S,找出它的最大回文子字符串. 你可以假定S的最大长度为1000, 并且这里存在唯一一个最大回文子字符串. 原文 Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 暴力搜索,O(n

LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法

LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之余全部耗在这上面了,只怪自己基础差.本文主要介绍使用Manacher线性算法来求解字符串的最长回文子字符串. 题目 Given a string S, find the longest palindromic substring in S. You may assume that the maxim

LeetCode #5 Longest Palindromic Substring (M)

[Problem] Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. [Analysis] 这题的思路有很多种,网上也有各种讨论.这里我采用的是个人觉得比较好理解的一种利用Dynamic Progra

LeetCode:Longest Palindromic Substring

Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 动态规划解法 T(n) = O(n^2)  ,S(n) = O(n^2); Solutio

LeetCode OJ:Longest Palindromic Substring(最长的回文字串)

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 玩了两天dota2,罪过罪过,还是应该老老实实刷题啊. 题目求得是最长的回文子串,这里使用的是暴力的解法,也就是解决两种回文"asdsa"以

Leetcode 5 Longest Palindromic Substring (最长回文子字符串)(动态规划)

Leetcode 5 题目描述 Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. 例子 Example 1: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. Example 2: Inp

LeetCode 5:Longest Palindromic Substring(最长回文串)

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 思路一:(超时)简单的截取长度为length(length递减)的字串,判断其是否为回文串.第一个满足要求的回文串最长. public class S