leetcode-mid-array-5. Longest Palindromic Substring
mycode 12.51%
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
length = len(s)
if length==0:
return ''
res = s[0]
for i in range(length-1):
for j in range(i+1,length):
if s[j] == s[i]:
temp = s[i:j+1]
if temp == temp[::-1]:
if len(temp) > len(res):
res = temp[:]
return res
参考:
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
n = len(s)
maxl, start = 0, 0
for i in range(n):
if i - maxl >= 1 and s[i-maxl-1:i+1] == s[i-maxl-1:i+1][::-1]:
start = i - maxl - 1
maxl += 2
continue
if i - maxl >= 0 and s[i-maxl:i+1] == s[i-maxl:i+1][::-1]:
start = i - maxl
maxl += 1
return s[start: start + maxl]
leetcode-mid-array-5. Longest Palindromic Substring的更多相关文章
- leetcode 第五题 Longest Palindromic Substring (java)
Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...
- leetcode第五题--Longest Palindromic Substring
Problem:Given a string S, find the longest palindromic substring in S. You may assume that the maxim ...
- Leetcode:【DP】Longest Palindromic Substring 解题报告
Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...
- leetcode--5 Longest Palindromic Substring
1. 题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximu ...
- LeetCode(5)Longest Palindromic Substring
题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- [LeetCode][Python]Longest Palindromic Substring
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 【LeetCode】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
- Leetcode: Longest Palindromic Substring && Summary: Palindrome
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
随机推荐
- express接受ajax的发送post请求
两种常用的方式: $.ajax({ type: "POST", url: '/everdayPost', // data: JSON.parse({cont:data}),注意: ...
- 五、JVM — 类加载器
回顾一下类加载过程 类加载器总结 双亲委派模型 双亲委派模型介绍 双亲委派模型实现源码分析 双亲委派模型的好处 如果我们不想要双亲委派模型怎么办? 自定义类加载器 推荐 回顾一下类加载过程 类加载过程 ...
- PHPstorm快捷键介绍总结
如下所示: Eclipse快捷键 Ctrl+1 快速修复 Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt ...
- wordpress添加自定义菜单栏
1. 以下代码是wordpress的wp_nav_menu(); <?php $defaults = array( 'container'=>false, //父级div为空 'link_ ...
- React全家桶入门
http://blog.csdn.net/column/details/14545.html
- 原生js格式化json和格式化xml的方法
在工作中一直看各位前辈的博客解决各种问题,对我的帮助很大,非常感谢! 之前一直比较忙没有写博客,终于过年有了点空闲时间,可以把自己积累的东西分享下,笔记中的部分函数不是自己写的,都是工作中一点点积累的 ...
- 关于jsp 获得当前绝对路径的方法
方法1) request.getRequestURL(); 方法2) request.getScheme()+"://"+request.getServerName()+&quo ...
- Linux vim程序编辑器
Tips: 在 vi 里面, [tab] 这个按钮所得到的结果与空格符所得到的结果是不一样的,特别强调一下! 一般模式 移动光标 30↓ 向下移动30行 40→ 向右移动40个字符 gg 移动到档案第 ...
- Linux架构之Nginx 动静分离
案例No.51:Nginx动静分离 1.web01配置静态资源 [root@web01 ~]# cd /etc/nginx/conf.d/#配置静态资源[root@web01 conf.d]# cat ...
- UVa 10294(polya 翻转与旋转)
Arif in Dhaka(First Love Part 2) Input: standard input Output: standard output Time Limit: 2 seconds ...