LeetCode 5. Longest Palindromic Substring & 回文字符串
Longest Palindromic Substring
回文这种简单的问题,在C里面印象很深啊。希望能一次过。
写的时候才想到有两种情况:
454(奇数位)
4554(偶数位)
第1次提交
class Solution:
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
print(s)
maxPadStr=''
# traverse each char
l=len(s)
for i,c in enumerate(s):
# left or right extend test
# j is length
j=1
while j <= l/2:
# left or right str
leftStr=s[i:i-j:-1] if (i-j)>=0 else s[i::-1]
rightStr=s[i:i+j] # odd
rightStr2=s[i+1:i+j+1] # even
# length
leftLen=len(leftStr)
rightLen=len(rightStr)
print(i,j,'left:',leftStr,'right:',rightStr,'right-even:',rightStr2)
if leftLen != rightLen:
break
if leftStr == rightStr and len(leftStr)*2-1>=len(maxPadStr):
#odd bit
maxPadStr=(leftStr[:0:-1]+rightStr) # 654,654 -> 45654
#print('odd',maxPadStr)
# 'cbbd' 时,left: b right: b right-even: b. odd even 都存在,但上面只会b,所以存在偶数位相等故不能elif
if leftStr == rightStr2 and len(leftStr)*2>=len(maxPadStr):
#even bit
maxPadStr=(leftStr[::-1]+rightStr2) # 654,654 -> 456654
#print('even',maxPadStr)
j+=1
return maxPadStr
if __name__ == "__main__":
data = [
{
"input":"babad",
"output":"bab", # /'aba'
},{
"input":"cbbd",
"output":"bb"
},{
"input":"123456654",
"output":"456654"
},{
"input":"14565422",
"output":"45654"
}
];
for d in data:
result=Solution().longestPalindrome(d['input'])
print(result)
if result==d['output']:
print("--- ok ---")
else:
print("--- error ---")
Wrong Answer:
Input:
"a"
Output:
""
Expected:
"a"
咦,这种一个字母早都考虑到了啊。
出在了while的条件上:j <= l/2:
哈哈,一半长度的直观出发哈哈哈l/2,没错,只是把1个字符就排除掉了,改成这样j<=l//2+1
。
第2次提交
pass
while j <= l//2+1:
pass
Accepted!
LeetCode 5. Longest Palindromic Substring & 回文字符串的更多相关文章
- Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)
Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...
- LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法
LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...
- [LeetCode] 125. Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- 求最长回文子串 - leetcode 5. Longest Palindromic Substring
写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...
- [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 ...
- leetcode 5 :Longest Palindromic Substring 找出最长回文子串
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- [leetcode]5. Longest Palindromic Substring最长回文子串
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
随机推荐
- Hbase Shell命令详解+API操作
HBase Shell 操作 3.1 基本操作1.进入 HBase 客户端命令行,在hbase-2.1.3目录下 bin/hbase shell 2.查看帮助命令 hbase(main):001:0& ...
- SpringBoot+Maven 多模块项目的构建、运行、打包
SpringBoot+Maven 多模块项目的构建.运行.打包 https://blog.csdn.net/zekeTao/article/details/79413919
- Django REST framework 总结(附源码剖析)
Django 的 CBV&FBV Django FBV, function base view 视图里使用函数处理请求 url url(r‘^users/‘, views.users), v ...
- 查看耗时长,CPU 100% 的SQL
[session_id], [request_id], [start_time] AS '开始时间', [status] AS '状态', [command] AS '命令', dest.[text] ...
- WINDOWS 命令行 串口 COM 发送数据
WINDOWS 命令 串口 数据 type con>com1 回车. com1 为想发送的串口. 输入字符并回车即可.
- NutzWk 开发框架
官网: https://wizzer.cn/ 源码下载: https://github.com/wizzercn/NutzWk/ https://gitee.com/wizzer/NutzWk Int ...
- 黄聪:windowss7显示桌面图标设置在任务栏的解决办法
1.新建一个本文文档,将以下内容复制进去: [Shell] Command=2 IconFile=explorer.exe,3 [Taskbar] Command=ToggleDesktop 2.将该 ...
- 【剑指offer】调整数组数字位置
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. *思路:遍历数组,找到第一个偶 ...
- servlet.xml 出现 Referenced file contains errors(http://.......)
问题描述: 打开Eclipse突然发现Web工程的servlet.xml突然报了红叉叉,错误信息如下: Referenced file contains errors (http://www.spri ...
- Flume的基本概念
Flume 概念 Flume 最早是Cludera提供的日志收集系统,后贡献给Apache.所以目前是Apache下的项目,Flume支持在日志系统中指定各类数据发送方,用于收集数据. Flume 是 ...