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. Android 第三方应用接入微信平台研究情况分享

    微信平台开放后倒是挺火的,许多第三方应用都想试下接入微信这个平台,毕竟可以利用微信建立起来的关系链来拓展自己的应用还是挺不错的 最近由于实习需要也在研究这个东西,这里把我的整个研究情况给出来 微信平台 ...

  2. webapp前端性能优化规范

    加载优化 合并css javascript 合并小图片 使用雪碧图 缓存一切可缓存的资源 使用长的cache 使用外链式引用css,javascript 压缩HTML,CSS,JAVASCRPT 启用 ...

  3. Tag It 一款 Jquery控件,当你在文本框中输入逗号时,自动帮你分隔开相关内容

    Demo地址:http://webspirited.com/tagit/ 使用方法: 除了JQuery脚本外,下面的脚本也是必须的,这些脚本你都可以去GitHub下载:https://github.c ...

  4. 闭包和let块级作用域

    还是先从一个题目开始: 写一个隔1s输出数组的一项的函数. 如果可以用ES6语法,则可以这么写: function print (arr) { for (let i = 0; i < arr.l ...

  5. MySQL优化之Explain命令解读

    简述: explain为mysql提供语句的执行计划信息.可以应用在select.delete.insert.update和place语句上.explain的执行计划,只是作为语句执行过程的一个参考, ...

  6. YouCompleteMe

    需要配套的.vimrc :sw:.h与.cpp切换 Issue:YouCompleteMe unavailable no module named future cd .vim/Vundle/YouC ...

  7. Wireshark工具抓包的数据包分析

    Wireshark(前称Ethereal)是一个网络封包分析软件.网络封包分析软件的功能是撷取网络封包,并尽可能显示出最为详细的网络封包资料. Wireshark使用WinPCAP作为接口,直接与网卡 ...

  8. 【Spark】算子

    1. mapWith mapWith(i => i*10)((a,b) => b+2) (拿到分区号)(a是每次取到的RDD中的元素,b接收i*10的结果) 2. flatMapWith ...

  9. 我的Tmux学习笔记

    0. 修改指令前缀 // ~/.tmux.conf ubind C-b set -g prefix C-a 1. 新建会话 tmux tmux new -s session-name // 可以设置会 ...

  10. 实验吧web登陆一下好吗

    知识点: 万能密码'=' 一道登陆型的SQL注入题目,这种题目一般有四种题型: 1.在username中通过union联查select一个已知字符来与传递的password匹配完成登陆 2.使用万能密 ...