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. WinCE下的串口通信开发(VS2005,VB.Net,VC++)

    WinCE下的串口通信开发(VS2005,VB.Net,VC++)   WinCE下的串口通信开发 一.利用Visual Basic 开发很简单,因为有现成的控件可以直接调用 以VS2005为例,首先 ...

  2. qt+vs2005新建配置不自动加载Generated Files进工程(个人备份)

    工程右键Qt Project Settings 的Moc Directory路径删除 确定,再进入将删除路径加上

  3. 用js写水仙花数

    ...js   //输入一个三位数,水仙花数就是个位的三次方+十为的三次方+百位的三次方之和等于本身 console.log('请输入一个三位数:'); let a = readline.questi ...

  4. idea操作 clone项目、 import项目所有注解全部报错

    操作:从现有的git上边clone项目,前提是开发工具,开发环境都一样错误类型:所有的注解全部报错 原因: 是选择了Create from existing source 一路Next下去,Maven ...

  5. Dokcer-ce安装脚本

    安装docker #!/bin/bash # coding: utf- # Copyright (c) set -e #返回值为0时,退出脚本 echo "1. 备份yum" { ...

  6. iOS之利用runtime,避免可变数组和可变字典为nil或者数组越界导致的崩溃

    NSArray.NSMutableArray.NSDictionary.NSMutableDictionary.是我们的在iOS开发中非常常用的类.当然,在享受这些类的便利的同时,它们也给我们带来一些 ...

  7. 基于jQuery+JSON的省市县 二级 三级 联动效果

    省市区联动下拉效果在WEB中应用非常广泛,尤其在一些会员信息系统.电商网站最为常见.开发者一般使用Ajax实现无刷新下拉联动.本文将讲述,利用jQuery插件,通过读取JSON数据,实现无刷新动态下拉 ...

  8. 帝国cms教程父栏目和子栏目都能在当前栏目高亮

    首先在/e/class/userfun.php这个文件里面加上下面代码.上面父栏目的,下面子栏目的.红色代表css样式.自定义吧 function currentPage($classid,$this ...

  9. 【NXP开发板应用—智能插排】4. PWM驱动

    [前言] 首先感谢深圳市米尔科技有限公司举办的这次活动并予以本人参加这次活动的机会,以往接触过嵌入式,但那都是皮毛,最多刷个系统之类的,可以说对于嵌入式系统开发这件事情是相当非常陌生的,这次活动为我提 ...

  10. python应用:爬虫框架Scrapy系统学习第三篇——初识scrapy

    scrapy的最通用的爬虫流程:UR2IM U:URL R2:Request 以及 Response I:Item M:More URL 在scrapy shell中打开服务器一个网页 cmd中执行: ...