leetcode Longest Palindromic Substring python

class Solution(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        lenStr = len(s)
        if lenStr <= 0:
            return 0
        maxLen = 0
        tmpLen = 0
        tmpRes = ‘‘
        for i in range(0,lenStr):

            #odd numbers
            j=0
            tmpLen=0
            while i-j >= 0 and i+j < lenStr and s[i-j] == s[i+j]:
                tmpLen = 2*j +1
                j+=1
            if tmpLen > maxLen:
                maxLen = tmpLen
          #i-j+1 plus one because first: j+=1 then judge s[i-j] == s[i+i]
                #so when the while cycle stop, the j is bigger one than the j that make while condition establish
                tmpRes = s[i-j+1:i+j]
            #even numbers
            j=0
            tmpLen=0
            while i-j>=0 and i+j+1 < lenStr and s[i-j] == s[i+j+1]:
                tmpLen = 2*j + 2
                j+=1
            if tmpLen > maxLen:
                maxLen = tmpLen
                tmpRes = s[i-j+1:i+j+1]
        return tmpRes
 
时间: 2024-10-27 04:37:52

leetcode Longest Palindromic Substring python的相关文章

LeetCode &quot;Longest Palindromic Substring&quot; - 1D DP

2D DP is an intuitive solution of course, but I got an MLE error, so I simplified it into a 1D DP: class Solution { public: void goDp(vector<int> &dp, int &maxLen, int &is, int &ie, int startLen, int len, string &s) { for(int ile

[LeetCode] Longest Palindromic Substring [14]

题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum length ofS is 1000, and there exists one unique longest palindromic substring. 原题链接 解题思路 最长回文字串,相信做过Palindrome Partitioning II 这个题的同学应该可以很快做出来.没错,这个题还可以

Leetcode: Longest Palindromic Substring. java

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. 动态规划public class Solution { public String longestPalindrome(String s) { if

Leetcode: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. 翻译:求给定

[Leetcode] 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. 做这道题之前要先了解什么是回文子串.回文串通俗的解释是,分别从字符串两端开始遍历,得到的结果相同,如"abba",从两端的遍历结果都是:&q

[leetcode]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. 基本思想: 本题的navie解法是对每个(i,j) i<=j 测试是否是回文串,并记录最大的情况.O(n^3) 如果从回文串的中心考虑往两

LeetCode——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,找出当中的最长回文字符子串. 1.枚举全部子串,并推断是否是回文串同一时候记录最大长度.超时. //找出全部子串,并推断是否是回文串

[LeetCode] 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. http://fisherlei.blogspot.com/2012/12/leetcode-longest-palindromic-substrin

leetcode longest palindromic substring (medium) /java

最长回文字串 上题: 测试用例中,注意aaabaaaa. 但是我time limit exceeded.用了极暴力的方法解.(三层循环)找每一个字符的最长回文字串. 1 /** 2 * 最长回文子串 3 * 2017-5-7 4 **/ 5 6 import java.io.*; 7 import java.util.*; 8 import java.lang.*; 9 10 public class Solution 11 { 12 public static String longestPa