题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

解题:参考网上大神做法,解题如下:

class Solution:

    def get_palindromic(self, s, k, l):
s_len = len(s)
while k >= 0 and l < s_len and s[k] == s[l]:
k -= 1
l += 1
return s[k+1:l] def longestPalindrome(self, s):
L_palindromic = ''
for i in range(len(s)):
temp_palindromic1 = self.get_palindromic(s, i, i)
if len(temp_palindromic1) > len(L_palindromic):
L_palindromic = temp_palindromic1 temp_palindromic2 = self.get_palindromic(s, i, i+1)
if len(temp_palindromic2) > len(L_palindromic):
L_palindromic = temp_palindromic2 return L_palindromic

leetcode解题—Longest Palindromic Substring的更多相关文章

  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】Longest Palindromic Substring 解题报告

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

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

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

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

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

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

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

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

  8. Java [leetcode 5] Longest Palindromic Substring

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

  9. [LeetCode][Python]Longest Palindromic Substring

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

随机推荐

  1. IE jquery mouseenter,mouseover超奇葩问题

    做了个项目,结构很简单 <div class="index-main" data-url="./img/index_default.jpg"> &l ...

  2. JavaScript标准Selection操作

    简介 术语 属性 方法 document.activeElement document.designMode = 'on'; 简介 selection是对当前激活选中区(即高亮文本)进行操作. 在非I ...

  3. SSH电力项目

    第一步:创建测试表Elec_Text: create table Elec_Text(textID varchar(50) not null primary key,textName varchar( ...

  4. 深入理解Binder(一),从AIDL谈起

    打算写一篇Binder介绍的文章,仔细想想一篇文章貌似很难厘清,于是我打算从AIDL入手,本篇先来简单介绍下AIDL的使用,然后在此基础上我们继续来研究Binder的工作原理.小伙伴们都知道,AIDL ...

  5. Android(java)学习笔记137:Android中SimpleAdapter,ArrayAdapter和BaseAdapter常见的适配器

    1.SimpleAdapter(BaseAdapter子类扩展类): simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片)等.可以显示比较复杂的列表, ...

  6. 3D分析之Functional Surface工具箱(转)

    来自:http://blog.csdn.net/kikitamoon/article/details/8195797 1. Add Surface Information(添加表面信息) 向点.线或面 ...

  7. selendroid项目实战3 selendroid driver初始化失败问题

    小米4/LG手机作为测试用机,随着测试时间变长,driver初始化失败率越来越高. 分析: 1.手机原因: 从小米换到LG,刚开始问题确实减少了,但是时间一长,又出现类似问题,提示Connect re ...

  8. 关于datalist

    http://www.cnblogs.com/shipfi/archive/2009/10/19/1585705.html

  9. 【linux错误解决】MySQL远程连接ERROR 2003 (HY000):Can't connect to MySQL server on'XXXXX'的问题

    问题描述: 从一台linux远程连接另一台linux上的MySQL, 出现ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx.xxx.x ...

  10. VS 2015 (RC)下Entitiy Framework 升级后CodeFirst命令不能使用

    前阵子闲的没事吧自己项目的Entitiy Framework 升级到6.1.3了,然后紧接着问题来了: Join-Path : 无法将参数绑定到参数“Path”,因为该参数是空值. 所在位置 F:\S ...