[leetcode] 5.Longest Palindromic Substring-1
开始觉得挺简单的 写完发现这个时间超限了:
class Solution:
def longestPalindrome(self, s: str) -> str:
# longest palindromic substring lenth < 1000
#NULL
string = s
if len(string) == 0:
return ''
# ---
# regular
# a not empty string must have 1 letterd palindromic substring:string[0]
longestPalindSubStringLength = 1
longestPalindSubString = string[0]
for start in range(len(string)):
for end in range(start + 1, len(string) +1): #[] operator is right open interval,will not count "end" if no +1
# one letter
if len(string[start:end]) == 1:
continue
if (self.isPalind(string[start:end])):
if len(string[start:end]) > longestPalindSubStringLength:# > will get bab ;>= will get aba. I prefer simple one
longestPalindSubStringLength = len(string[start:end])
longestPalindSubString = string[start:end]
return longestPalindSubString def isPalind(self, substring: str) -> bool:
#notice substring >= 2
substringlength = len(substring) looplimit = substringlength // 2
for index in range(0, looplimit):
if substring[index] != substring[substringlength-1 -index]: # axisymmetric
return False
else:
pass
# run to here without return mean palindromic
return True
"abababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababa"
用pycharm跑了下 要4秒,结果就是它自己。[testcase:85]
发现之前判断轴对称的方法太低级了 不用逐位判断 轴对称的前一半和后一半的逆序是同一字符串即可。
class Solution:
def longestPalindrome(self, s: str) -> str:
# longest palindromic substring lenth < 1000
#NULL
string = s
if len(string) == 0:
return ''
# ---
# regular
# a not empty string must have 1 letterd palindromic substring:string[0]
longestPalindSubStringLength = 1
longestPalindSubString = string[0]
for start in range(len(string)):
for end in range(start + 1, len(string) +1): #[] operator is right open interval,will not count "end" if no +1
# one letter
if len(string[start:end]) == 1:
continue
if (self.isPalind(string[start:end])):
if len(string[start:end]) > longestPalindSubStringLength: #> mean bab;>= mean aba
longestPalindSubStringLength = len(string[start:end])
longestPalindSubString = string[start:end]
return longestPalindSubString def isPalind(self, substring: str) -> bool:
#notice substring >= 2
substringlength = len(substring) if(substringlength %2) == 1:
#odd
temp1 = substring[0:substringlength // 2 +1]
temp2 = substring[substringlength:substringlength // 2 - 1:-1]
if temp1 == temp2:
return True
else:
return False
else:
#even
temp1 = substring[0:substringlength//2]
temp2 = substring[substringlength:substringlength//2 -1:-1]
if temp1 == temp2:
return True
else:
return False
testcase:95
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"
f比g多一个
testcase:98
def longestPalindrome(self, s: str) -> str:
# longest palindromic substring lenth < 1000
# NULL
if len(s) == 0:
return ''
# ---
# regular
# a not empty string must have 1 letterd palindromic substring:string[0]
longestPalindSubStringLength = 1
longestPalindSubString = s[0]
for start in range(len(s)):
for end in range(start + 1, len(s) + 1): # [] operator is right open interval,will not count "end" if no +1
# left to right for end in range(start + 1, len(s) + 1)
#from right to left range(length -1,-)
# one letter
if len(s[start:end]) == 1 or len(s[start:end]) <= longestPalindSubStringLength:
continue
if (self.isPalind(s[start:end])):
if len(s[start:end]) > longestPalindSubStringLength:
longestPalindSubStringLength = len(s[start:end])
longestPalindSubString = s[start:end]
return longestPalindSubString def isPalind(self, substring: str) -> bool:
# notice substring >= 2
substringlength = len(substring) if (substringlength % 2) == 1:
# odd
temp1 = substring[0:substringlength // 2 + 1]
temp2 = substring[substringlength:substringlength // 2 - 1:-1]
if temp1 == temp2:
return True
else:
return False
else:
# even
temp1 = substring[0:substringlength // 2]
temp2 = substring[substringlength:substringlength // 2 - 1:-1]
if temp1 == temp2:
return True
else:
return False
单个testcase耗时从500ms降到了200ms 但是总是到100/108的时候报TLE,换方法,下篇再说。
[leetcode] 5.Longest Palindromic Substring-1的更多相关文章
- LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法
LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...
- Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)
Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...
- 求最长回文子串 - leetcode 5. Longest Palindromic Substring
写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...
- LeetCode 5 Longest Palindromic Substring(最长子序列)
题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...
- 【JAVA、C++】LeetCode 005 Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 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] 5. Longest Palindromic Substring 最长回文子串
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
随机推荐
- Spring Boot Security 整合 JWT 实现 无状态的分布式API接口
简介 JSON Web Token(缩写 JWT)是目前最流行的跨域认证解决方案.JSON Web Token 入门教程 - 阮一峰,这篇文章可以帮你了解JWT的概念.本文重点讲解Spring Boo ...
- 2014年第五届蓝桥杯javaB组 试题 答案 解析
1.武功秘籍 小明到X山洞探险,捡到一本有破损的武功秘籍(2000多页!当然是伪造的).他注意到:书的第10页和第11页在同一张纸上,但第11页和第12页不在同一张纸上. 小明只想练习该书的第81页到 ...
- 解决IE6-IE7下li上下间距变大问题
在IE6/7下li会向下产生大约2px的外边距 解决方法:li{vertical-align:top;}或者 li{vertical-align:bottom;} 解决问题!
- H5、C3、ES6的新特性
H5的新特性 1.语义化标签 有利于SEO,有助于爬虫抓取更多的有效信息,爬虫是依赖于标签来确定上下文和各个关键字的权重. 语义化的HTML在没有CSS的情况下也能呈现较好的内容结构与代码结构 方便其 ...
- 网络爬虫BeautifulSoup库的使用
使用BeautifulSoup库提取HTML页面信息 #!/usr/bin/python3 import requests from bs4 import BeautifulSoup url='htt ...
- 浏览器与android移动端视频互播技术实现
手机端与平台之间的视频直播功能,主要通过集成多种开源视频框架以及采购第三方视频直播服务器产品来实现预定业务需求.视频直播对话功能的实现,主要经历了三个阶段:利用开源视频框架实现视频直播.采购第三方视频 ...
- Numpy库的学习(二)
今天来继续学习一下Numpy库的使用 接着昨天的内容继续 在Numpy中,我们如果想要进行一个判断使用“==” 我们来看下面的代码 vector = np.array([5,10,15,20,25]) ...
- 『Shell编程』学习记录(2)
例1.文件io #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include & ...
- Postgres中文分词
环境 CentOS Linux release 7.2.1511 (Core) 安装Postgres 安装postgres很简单 yum安装 sudo yum install postgresql-s ...
- Eclipse里JAR文件的打包和使用
作用:用于封装class.properties文件,是文件封装的最小单元: 包含Java类的普通库.资源(resources).辅助文件(auxiliary files)等. 可以将程 ...