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.

Example:

Input: "babad"

Output: "bab"

Note: "aba" is also a valid answer.

Example:

Input: "cbbd"

Output: "bb"

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

class Solution1:

    def longestPalindrome(self, s):

        """

        :type s: str

        :rtype: str

        """

        res = ""

        for i in range(len(s)):

            j = i + len(res) + 1

            while j <= len(s):

                cur = s[i:j]

                if len(cur) > len(res) and cur == cur[::-1]:

                    res = cur

                j += 1

        return res

class Solution2:

    def longestPalindrome(self, s):

        if len(s) == 0:

            return ""

        maxLen = 1

        start = 0

        for i in range(len(s)):

            cur = s[i - maxLen - 1:i + 1]

            if i - maxLen >= 1 and cur == cur[::-1]:

                start = i - maxLen - 1

                maxLen += 2

                continue

            cur = s[i - maxLen:i + 1]

            if i - maxLen >= 0 and cur == cur[::-1]:

                start = i - maxLen

                maxLen += 1

        return s[start:start + maxLen]

s = "babad"

#s = "cbbd"

solution = Solution2()

res = solution.longestPalindrome(s)

# print(res)

来自为知笔记(Wiz)

原文地址:https://www.cnblogs.com/xiejunzhao/p/8445814.html

时间: 2024-11-06 21:45:45

5. Longest Palindromic Substring 最长的回文子串的相关文章

LeetCode:5_Longest Palindromic Substring | 最长的回文子串 | Medium

题目: 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. 解题思路:1.简单思路:暴力破解法,时间复杂度O(n^3),肯定通不过. 2.动态规划法:(一般含“最XX”等优化词义的题意味着都可以动态规划

[C++]LeetCode: 99 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. 思路:题目要求的s的一个最长回文子串.暴力解决办法就是枚举所有的子串,再对每个子串进行回文判断.进行剪枝,我们考虑可以使用动态规划来避免重复的判

Java Longest Palindromic Substring(最长回文字符串)

假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic string.如aba,或者abba.本题是这种,给定输入一个字符串.要求输出一个子串,使得子串是最长的padromic string. 下边提供3种思路 1.两側比較法 以abba这样一个字符串为例来看,abba中,一共同拥有偶数个字.第1位=倒数第1位.第2位=倒数第2位......第N位=倒数第N位 以aba这样一个字符串为例来看,aba中.一共同拥有奇数个字符.排除掉正中间的那个字符后,第1位=倒数第1

[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. 最长回文子串Longest palindromic substring, 最长回文子串或最长对称因子问题是在一个字符串中查找一个最长连续子串,这个子串

Longest Palindromic Substring(最长回文子串)

Description: 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: &q

LeetCode-5:Longest Palindromic Substring(最长回文子字符串)

描述:给一个字符串s,查找它的最长的回文子串.s的长度不超过1000. Input: "babad" Output: "bab" Note: "aba" is also a valid answer. 我是采用动态规划解决此题的.官方的solutions中提供了几种思路,包括我使用的DP.这里摘要如下: 思路1: 将s反转得到s',然后查找s和s'的最长公共子串substring,那么substring就是最长回文子串.比如:s = "

manacher算法处理最长的回文子串(一)

引言 相信大家都玩过折叠纸张,如果把回文串相当于折叠一个A4纸,比如ABCDDCBA就是沿着中轴线(D与D之间)对折重合,那么这个就是一个回文串.或者是ABCDEDCBA的中轴线就是E,那么沿着中轴线对折也是重合的,所以这个字符串也是一个回文串. 判断一个字符串中的最长回文子串,我们可以对每个字符的两边进行比较,还是如何ABCDEDCBA,在A,B,C,D分别为中心轴向两边扩展的回文子串长度都是1,就是它自己本身,当扫描以E为中心轴时,那么这个回文的最长子串时9,也就是这个字符串本身.然而当用相

manacher算法处理最长的回文子串(二)

在上篇<manacher算法处理最长的回文子串(一)>解释了manacher算法的原理,接着给该算法,该程序在leetcode的最长回文子串中通过.首先manacher算法维护3个变量.一个名为radius[i]的数组,表示以i为中心轴承的回文子串的半径,如abcdcba中,字符d的下标为4,则他的radius[4]=3,下标的为0的a的半径为radius[0]=0,即中心轴不考虑其中.一个idx表示上一次以idx为中心轴的回文.如果当以i为中心的回文在以idx为中心的回文内.则idx不更新,

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