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. angular1的复选框指令--checklistModel

    这个指令可以改变一组checkbox的model格式,提交的时候格式为[x,y,z,...] //复选框指令 .directive('checklistModel', ['$parse', '$com ...

  2. 随机数使用不当引发的生产bug

    前几天负责的理财产品线上出现问题:一客户赎回失败,查询交易记录时显示某条交易记录为其他人的卡号. 交易的链路如下: 出现该问题后,我们对日志进行了分析,发现主站收到的两笔流水号完全相同,然而主站却没有 ...

  3. NLP语言模型

    语言模型: I. 基本思想 区别于其他大多数检索模型从查询到文档(即给定用户查询,如何找出相关的文档), 语言模型由文档到查询,即为每个文档建立不同的语言模型,判断由文档生成用户查 询的可能性有多大, ...

  4. React-Reflux 基础分享

    因工作需要使用 React + Reflux 开发,最近几天都在努力学习着,特别是 Reflux,在网上查找的许多资料和 github 上的文档年代都有点久远,JavaScript 按照目前的节奏,更 ...

  5. mysql 8.0.12安装步骤

    首先从官网下载压缩包: 解压压缩包到指定目录,在目录下新建my.ini,配置内容如下; [mysqld]  # 设置3306端口  port=3306  # 设置mysql的安装目录  basedir ...

  6. CSS中的动画

    1.transition 在CSS3中,可以通过transition为元素从一种样式变换为另外一种样式的过程添加效果. transition为简写属性,用于在一个属性中设置四个过渡属性,分别是: tr ...

  7. day 19 反射

    1.isinstance, type, issubclass 的含义 isinstance:  判断你给对象时候是xxx类型的.(向上判断) type: 返回xxx对象的数据类型 issubclass ...

  8. centos7.3 vsftpd 多用户配置

    1. 安装vsftpd及pam认证服务软件 yum install vsftpd* -y yum install pam* libdb-utils libdb* --skip-broken -y #设 ...

  9. STM32(11)——DMA

    简介: DMA:Direct Memory Access,直接存储器访问.DMA传输数据从一个地址空间复制到另外一个地址空间.当CPU初始化这个传输动作,传输动作本身就是DMA控制器来实现和完成.典型 ...

  10. 一个 lambda 表达式引起的思考

    一个 lambda表达式 引起的思考 fun = [lambda x: x*i for i in range(4)] for item in fun:    print(item(1)) 全文都是抄来 ...