LeetCode 5. Longest Palindromic Substring

9人阅读 评论(0) 收藏 举报
分类:

题目

这里写图片描述

思路

dp,构造上三角矩阵。

代码

class Solution:
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        if not s: return null
        length = len(s)
        #init
        dp = [[0 for i in range(length)] for j in range(length)]
        #update dp
        for i in range(length)[::-1]:
            for j in range(i, length):
                if i == j:
                    dp[i][j] = 1
                    continue
                if s[i] == s[j]:
                    if j == i + 1:
                        dp[i][j] = 2
                    elif dp[i + 1][j - 1]:
                        dp[i][j] = dp[i + 1][j - 1] + 2
        maxi = 0; maxj = 0; maxNum = 0
        for i in range(length):
            for j in range(i, length):
                if dp[i][j] > maxNum:
                    maxi = i; maxj = j; maxNum = dp[i][j]
        return s[maxi:maxj + 1]
查看评论

LeetCode(5) Longest Palindromic Substring

如果一个字符串从左向右写和从右向左写是一样的,这样的字符串就叫做palindromic string,如aba,或者abba。给定输入一个字符串,要输出一个子串,使得子串是输入字符串的最长的padro...
  • feliciafay
  • feliciafay
  • 2013-11-27 14:00:30
  • 18032

LeetCode 5 Longest Palindromic Substring(C,C++,Python,Java)

Problem: Given a string S, find the longest palindromic substring in S. You may assume that the ma...
  • runningtortoises
  • runningtortoises
  • 2015-05-06 15:38:22
  • 1499

LeetCode5——Longest Palindromic Substring 最易懂的解法

Given a string S, find the longest palindromic substring in S. You may assume that the maximum l...
  • booirror
  • booirror
  • 2016-01-22 23:05:18
  • 1049

[LeetCode][5]Longest Palindromic Substring解析 -Java实现

Q: Given a string S, find the longest palindromic substring in S. You may assume that the maxim...
  • u014629433
  • u014629433
  • 2016-06-05 13:48:00
  • 544

[LeetCode]5 Longest Palindromic Substring(C++,Python实现)

LeetCode OJ的第五题,如果有问题或者给我指点欢迎来信讨论ms08.shiroh@gmail.com 题目描述...
  • Shiroh_ms08
  • Shiroh_ms08
  • 2014-05-23 21:28:36
  • 3302

Leetcode 5. Longest Palindromic Substring 新补充Manacher算法

5. Longest Palindromic Substring Total Accepted: 98755 Total Submissions: 434700 Difficulty: Medi...
  • fantasiasango
  • fantasiasango
  • 2016-03-15 09:34:45
  • 618

LeetCode 5:Longest Palindromic Substring

Given a string S, find the longest palindromic substring in S. You may assume that the maximum len...
  • sunao2002002
  • sunao2002002
  • 2015-05-12 00:38:14
  • 2708

Longest Palindromic Substring -- LeetCode

原题链接: http://oj.leetcode.com/problems/longest-palindromic-substring/  这道题是比较常考的题目,求回文子串,一般有两种方法。 第一种...
  • linhuanmars
  • linhuanmars
  • 2014-03-10 03:49:51
  • 20435

LeetCode第五题,Longest Palindromic Substring

题目原文: Given a string S, find the longest palindromic substring in S. You may assume that the maximu...
  • hu1020935219
  • hu1020935219
  • 2014-08-05 14:29:41
  • 2887

[leetcode: Python]5.Longest Palindromic Substring

题目: Given a string s, find the longest palindromic substring in s. You may assume that the maximum ...
  • NXHYD
  • NXHYD
  • 2017-05-17 10:32:45
  • 386
    个人资料
    持之以恒
    等级:
    访问量: 8万+
    积分: 5720
    排名: 5682