题目: 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. 出栈入栈动画demo

    项目做了一个切换界面动画的功能,用到了出栈入栈的,写了一个demo package com.myron.stackview; import java.util.Stack; import androi ...

  2. google guava 基本工具

    近期在项目中用到了google中的cache了解到guava里面的一些工具类和对集合的操作,封装的都比较下,没有时间自己去写,先做个标记, 参考文章如下: http://macrochen.iteye ...

  3. Cummins INSITE locked and ask for verification code

    Some Cummins INSITE users turn to our engineer with a same question: INSITE has detected an invalid ...

  4. 让ubuntu开启ssh服务以及让vi/vim正常使用方向键与退格键

    VIM 修复方法: 安装vim full版本,在full版本下键盘正常,安装好后同样使用vi命令.ubuntu预装的是vim tiny版本,而需要的是vim full版本.执行下面的语句安装vim f ...

  5. php入门实现留言板

    首先由一个文本文档read.txt liulan.html <!doctype html> <html lang="en"> <head> &l ...

  6. JAXB - XML Schema Types, Defining Types for XML Elements Without Content

    Types for XML elements are constructed using xsd:complexType, even if they do not have content. The ...

  7. Android richtext

    在项目开发过程中经常会遇到很多需要显示不同样式的,不同风格的文本信息:对此可以使用多个TextView来分别设置自已想要的样式以满足需求,但是使用多个TextView的方式不太好:使用多个TextVi ...

  8. 启发式搜索 A*算法的OC 实现

    前两天重新学习了下A*算法,上次学习A*算法已经是5年前了,看到网上铺天盖地的A*算法都是C.C++等等其他语言的,就是没有OC 的,所以抽空写了一份.今天太晚了就不说明A*算法的细节了,大家如果想学 ...

  9. sea.js总结

    SeaJS是一个遵循CommonJS规范的JavaScript模块加载框架. 参考以下网址进行详细学习: https://segmentfault.com/a/1190000000357191?pag ...

  10. 如何在Markdown、HTML编辑器上输入一些特殊字符

    如何输入EntityCode 参考: 1.EntityCode 2.Common HTML entities used for typography 3.Latin Supplement-拉丁补充