码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode解题—Longest Palindromic Substring

时间:2015-07-04 18:11:21      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

题目: 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.

 

解题:参考网上大神做法,解题如下:

class Solution:

    def get_palindromic(self, s, k, l):
        s_len = len(s)
        while k >= 0 and l < s_len and s[k] == s[l]:
            k -= 1
            l += 1
        return s[k+1:l]

    def longestPalindrome(self, s):
        L_palindromic = ‘‘
        for i in range(len(s)):
            temp_palindromic1 = self.get_palindromic(s, i, i)
            if len(temp_palindromic1) > len(L_palindromic):
                L_palindromic = temp_palindromic1

            temp_palindromic2 = self.get_palindromic(s, i, i+1)
            if len(temp_palindromic2) > len(L_palindromic):
                L_palindromic = temp_palindromic2

        return L_palindromic

 

leetcode解题—Longest Palindromic Substring

标签:

原文地址:http://www.cnblogs.com/siriuswang/p/4620857.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!