想了很多方法 搞轴对称,算对称轴,偶数都搞出了一堆0.5在那加加减减,最后发现在移轴之前可能就返回了。

class Solution:
def longestPalindrome(self, s: str) -> str:
# longest palindromic substring lenth < 1000
# NULL
lengthS = len(s)
if lengthS == 0:
return ''
# ---
# regular
# a not empty string must have 1 letterd palindromic substring:string[0]
longestPalindSubStringLength = 1
longestPalindSubString = s[0]
if lengthS == 1:
return longestPalindSubString
#for any string,the longest palindoromic subtring is when the axis in the middle
if lengthS %2 == 1:
#even
axis = lengthS // 2
offset = [0]
for i in range(1, axis):
offset.append(i)
offset.append(-i)
for index in range(len(offset)): # move axis[0,+1,-1,+2,-2...]
currentaxis = axis + offset[index]
for decrease in range(0, currentaxis):
if (lengthS // 2 - currentaxis) > 0:
# axis at leftside
tempstr = s[decrease:2 * currentaxis - decrease]
tempstrlength = len(tempstr)
if tempstrlength < 2 or tempstrlength <longestPalindSubStringLength:
continue
if (self.isPalind(tempstr)):
if tempstrlength > longestPalindSubStringLength:
longestPalindSubString = tempstr
else:
# axis at rightside middle
tempstr = s[currentaxis - ((lengthS-1-decrease) - currentaxis):lengthS - decrease]
tempstrlength = len(tempstr)
if tempstrlength < 2 or tempstrlength <longestPalindSubStringLength:
continue
if (self.isPalind(tempstr)):
if tempstrlength > longestPalindSubStringLength:
longestPalindSubString = tempstr
else:
#odd
#---
#self middle have no axis in int. have to check first
axis = lengthS / 2 -0.5 #2.5
offset = []
if axis >1:
for i in range(1, int(axis + 0.5)):
offset.append(i - 0.5)
offset.append(-(i - 0.5))
for index in range(len(offset)): # move axis[0,+0.5,-0.5,+1.5,-1.5...]
currentaxis = int(axis + offset[index]) for decrease in range(0, currentaxis):
if (lengthS // 2 - currentaxis) > 0:
# axis at leftside
tempstr = s[decrease:2 * currentaxis - decrease]
tempstrlength = len(tempstr)
if tempstrlength < 2 or tempstrlength <longestPalindSubStringLength:
continue
if (self.isPalind(tempstr)):
if tempstrlength > longestPalindSubStringLength:
longestPalindSubString = tempstr
else:
# axis at rightside
tempstr = s[currentaxis - ((lengthS-1-decrease) - currentaxis):lengthS - decrease]
tempstrlength = len(tempstr)
if tempstrlength < 2 or tempstrlength < longestPalindSubStringLength:
continue
if (self.isPalind(tempstr)):
if tempstrlength > longestPalindSubStringLength:
longestPalindSubString = tempstr 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

testcase101:

"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

后来发现稍微做下优化 提前return就能达到时间限制。

103 / 103 test cases passed.
Status:

Accepted

Runtime: 8876 ms
Memory Usage: 13.2 MB
Submitted: 4 minutes ago
class Solution:
def longestPalindrome(self, s: str) -> str:
# longest palindromic substring lenth < 1000
# NULL
lengthS = len(s)
if lengthS == 0:
return ''
#all same
if lengthS == s.count(s[0]):
return s
# ---
# regular
# a not empty string must have 1 letterd palindromic substring:string[0]
longestPalindSubStringLength = 1
longestPalindSubString = s[0]
for start in range(lengthS):
for end in range(start + 1, lengthS + 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,-1)
currentstring = s[start:end]
currentstringLength = len(s[start:end])
# one letter
if len(currentstring) == 1 or currentstringLength <= longestPalindSubStringLength:
continue
if (self.isPalind(currentstring)):
if currentstringLength > longestPalindSubStringLength:
if end == lengthS:
return currentstring #speed up
longestPalindSubStringLength = currentstringLength
longestPalindSubString = currentstring 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

最后抄袭一个92ms的Manacher's Algorithm算法:

class Solution:
def longestPalindrome(self, s: str) -> str:
res = 0
p = ""
for i in range(0, len(s)): #
for d in [0,1]:
if s[i-res:i][::-1] == s[i+d:i+res+d]:
j = res+1
while (i-j >= 0) and (i+j+d) <= len(s) and s[i-j] == s[i+j+d-1]:
j += 1 if j > res+1 or d == 1:
p = s[i-j+1:i+j+d-1]
res = len(p) // 2 return p

[leetcode] 5.Longest Palindromic Substring-2的更多相关文章

  1. LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法

    LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...

  2. Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

    Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...

  3. 求最长回文子串 - leetcode 5. Longest Palindromic Substring

    写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...

  4. LeetCode 5 Longest Palindromic Substring(最长子序列)

    题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...

  5. 【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 ...

  6. leetcode:Longest Palindromic Substring(求最大的回文字符串)

    Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...

  7. [LeetCode][Python]Longest Palindromic Substring

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  8. 【LeetCode】Longest Palindromic Substring 解题报告

    DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...

  9. [LeetCode] 5. Longest Palindromic Substring 最长回文子串

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  10. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

随机推荐

  1. 《C#并发编程经典实例》学习笔记—2.2 返回完成的任务

    问题: 如何实现一个具有异步签名的同步方法. 从异步接口或基类继承代码,但希望用同步方式实现方法. 解释一下所谓的异步接口和异步基类.例如如下代码 interface IMyAsyncInterfac ...

  2. Spring入门(三):通过JavaConfig装配bean

    上一篇博客中,我们讲解了使用组件扫描和自动装配实现自动化装配bean,这也是最好的使用方式. 但是某些场景下,我们可能无法使用自动装配的功能,此时就不得不显式的配置bean. 比如我们引用了一个第三方 ...

  3. 基础概念PHP-FPM、FastCGI和CGI

    在搭建 LAMP/LNMP 服务器时,会经常遇到 PHP-FPM.FastCGI和CGI 这几个概念.如果对它们一知半解,很难搭建出高性能的服务器.接下来我们就以图形方式,解释这些概念之间的关系. 基 ...

  4. 解决PostGIS打开shp文件输入输出模块出现"找不到文件libintl-9.dll"的问题

    找到shp2pgsql-gui.exe这个程序的目录 复制一份libintl-8.dll副本,改名为libintl-9.dll即可.

  5. 亿级流量场景下,大型架构设计实现【全文检索高级搜索---ElasticSearch篇】-- 中

    1.Elasticsearch的基础分布式架构: 1.Elasticsearch对复杂分布式机制的透明隐藏特性2.Elasticsearch的垂直扩容与水平扩容3.增减或减少节点时的数据rebalan ...

  6. Android 注解框架对比

    Java的注解(Annotation)相当于一种标记,在程序中加入注解就等于为程序打上某种标记,标记可以加在包,类,属性,方法,本地变量上.然后你可以写一个注解处理器去解析处理这些注解(人称编译时注解 ...

  7. 使用Android服务,实现报警管理器和广播接收器

    介绍 几乎在所有平台上都有很多进程运行背景,它们被称为服务.可能在Android平台中有一些服务可以执行长时间运行的操作,这些操作在处理时不需要用户交互. 在本文中,借助预定义的Android警报服务 ...

  8. 简单易懂的单元测试框架-gtest(二)

    简介     事件机制用于在案例运行前后添加一些操作(相当于挂钩函数).目前,gtest提供了三种等级的事件,分别: 全局级,所有案例执行的前后 TestSuite级,某一个案例集的前后 TestCa ...

  9. C学习笔记(逗号表达式)

    (1)书写: ① int i; i=(i=*,i*); printf("%d\n",i); i=60; ② int i; i=i=*,i*; printf("%d\n&q ...

  10. SQL Server系统表sysobjects介绍

    SQL Server系统表sysobjects介绍 sysobjects 表结构: 列名 数据类型 描述 name sysname 对象名,常用列 id int 对象标识号 xtype char(2) ...