首页 > 其他 > 详细

leetcode解题—Longest Palindromic Substring

时间:2015-07-04 18:11:16      阅读:254      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!