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 & 回文字符串的更多相关文章

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

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

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

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

  3. [LeetCode] 125. Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

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

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

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

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

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

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

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

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

  8. leetcode 5 :Longest Palindromic Substring 找出最长回文子串

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

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

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

随机推荐

  1. Git-打标签

    打标签同大多数 VCS 一样,Git 也可以对某一时间点上的版本打上标签.人们在发布某个软件版本(比如 v1.0 等等)的时候,经常这么做.本节我们一起来学习如何列出所有可用的标签,如何新建标签,以及 ...

  2. 语义分析之ansj_seg+word2vec的使用

    语义分析,我是一个初学者,有很多东西,需要理论和实践结合后,才能理解的相对清楚. 今天,我就在语义理解中基于背景语料的情况,实现语义上下文的预测,比如,我说“王宝强”,你会想到什么?别告诉没有“马蓉” ...

  3. CentOS7安装部署zabbix3.4操作记录

    CentOS7安装部署zabbix3.4操作记录 1.安装前准备 1.1 查看centos的系统版本 [root@zabbix ~]# cat /etc/redhat-release CentOS L ...

  4. js 实现滚动字幕

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Excel技巧--批量生成指定名称的文件夹

    当我要按excel表当中的名字来批量生成文件夹时,手动一个个制作很麻烦(特别是成百上千个时).于是我们可以这么做: 1.在名字右侧建立公式:"MD "&A2. 2.将公式拖 ...

  6. xe5 android 手机上使用sqlite [转]

    在android手机上怎样使用sqlite数据库,这里用Navigator实现 增删改查. 1.新建firemonkey mobile application 2.选择blank applicatio ...

  7. 移动互联网终端的touch事件,touchstart, touchend, touchmove 很棒的文章

    转载请注明: 转载自WEB前端开发(www.css119.com)-关注常见的WEB前端开发问题.最新的WEB前端开发技术(webApp开发.移动网站开发).最好的WEB前端开发工具和最全的WEB前端 ...

  8. PHP 函数获取文件名

    <?php // php 获取  文件名 function getExt($url){ $arr = parse_url($url); // URL 字符串予以解析,并将结果返回数组中 //pr ...

  9. 胖子哥的大数据之路(10)- 基于Hive构建数据仓库实例

    一.引言 基于Hive+Hadoop模式构建数据仓库,是大数据时代的一个不错的选择,本文以郑商所每日交易行情数据为案例,探讨数据Hive数据导入的操作实例. 二.源数据-每日行情数据 三.建表脚本 C ...

  10. AutoCAD的代替软件

    AutoCAD的代替软件Autocad2005以上版本都需要.net framework的支持,安装起来太麻烦,而且卡顿.以下几个软件可以代替Autocad,操作和插件基本都能兼容Autocad.1. ...