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"
 class Solution:
def solve(self,s,l,r):
while r<len(s) and l>= and s[l]==s[r]:
l-=
r+=
return s[l+:r]
def longestPalindrome(self,s):
if len(s) == :
return
max_string=''
for i in range(len(s)):
#奇数长度回文串如ababa
tmp=self.solve(s,i,i)
if len(tmp)>len(max_string):
max_string=tmp
#偶数回文串如abba tmp=self.solve(s,i,i+)
if len(tmp)>len(max_string):
max_string=tmp return max_string

Code

 def solve(s,l,r):
while r<len(s) and l>= and s[l]==s[r]:
l-=
r+=
return s[l+:r]
def longestPalindrome(s):
if len(s) == :
return
max_string=''
for i in range(len(s)):
#奇数长度回文串如ababa
tmp=solve(s,i,i)
if len(tmp)>len(max_string):
max_string=tmp
#偶数回文串如abba tmp=solve(s,i,i+)
if len(tmp)>len(max_string):
max_string=tmp return max_string print(longestPalindrome('ababa'))

Debug Code

(python)leetcode刷题笔记05 Longest Palindromic Substring的更多相关文章

  1. (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...

  2. 【leetcode刷题笔记】Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. 【leetcode刷题笔记】Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  4. 【leetcode刷题笔记】Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 题解:以strs[0]为模 ...

  5. 【leetcode刷题笔记】Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  6. (python)leetcode刷题笔记04 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...

  7. 【leetcode刷题笔记】Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  8. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  9. LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划

    题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...

随机推荐

  1. Extjs header column 自定义排序规则

    Extjs 的表格自带排序功能,这个功能在大部分情况下能够满足我们的需求,但是在某种情况下,例如IP排序,默认情况下,按照字符串进行排序, 此时我们需要自定义排序规则,这个时候就需要我们重写方法了, ...

  2. python Web开发之 WSGI & uwsgi & uWSGI

    首先弄清下面几个概念: WSGI 全称是Web Server Gateway Interface,WSGI不是服务器,python模块,框架,API或者任何软件,只是一种规范,描述web server ...

  3. 【oracle】关于创建表时用default指定默认值的坑

    刚开始学create table的时候没注意,学到后面发现可以指定默认值.于是写了如下语句: 当我查询的时候发现,查出来的结果是这样的.. 很纳闷有没有,我明明指定默认值了呀,为什么创建出来的表还是空 ...

  4. 浅谈Jquery和常用框架Vue变化

    区别 Vue数据与视图的分离 Vue数据驱动视图 Jquery 简单示例: <!DOCTYPE html> <html lang="en"> <hea ...

  5. 『ACM C++』 PTA 天梯赛练习集L1 | 040-41

    近期安排 校赛3.23天梯赛3.30华工校赛 4.21省赛 5.12 ------------------------------------------------L1-040----------- ...

  6. 执行SQL查询导致磁盘耗尽故障演示

            a fellow in IMG wechat group 2 met an error about running out of disk space when using MySQL ...

  7. md5加密+盐方式二

    这类md5+盐加密是属于自定义盐值的简单方法! 1.导入架包 2.调用方法 DigestUtils.md5Hex(password);//加密方法 举例 方式一: password=DigestUti ...

  8. AppleScript 快速入门

    AppleScript 快速入门 AppleScript 顾名思义是苹果开发的一套脚本语言,利用 AppleScript 在 macOS 系统上可以对其他程序进行操作,点击按钮.发送消息.模拟自动化执 ...

  9. 微信支付tp5.1集合

    多商户号微信支付 配置 自己改一改 逻辑 就好了! 写的菜 勿喷 extend下面 主要目录 多商户号 配置项 根据自己的需求更改 可能有一些地方存在BUG 自己调试一下 就OK了,别像一个麻瓜一样 ...

  10. Delphi的TValue探索(一)

    TValue是Delphi的RTTI系统的重要类型. 经过摸索,发现TValue功能强大,可以实现很多功能.本文章中所有程序采用XE3运行通过. 一.TValue结构 TValue定义在System. ...