Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example 1:                             Input: "babad"                         Output: "bab"                      Note: "aba" is also a valid answer.

Example 2:                             Input: "cbbd"                           Output: "bb"

思路:最简单的办法是使用暴力破解法,依此遍历每一个字符(基数情况)和相邻的两个字符(偶数情况)的情况来对比前后是最长相等的字符串,最后返回结果。但是时间复杂度太高为O(n3),如果字符串太长会直接超时。

    第二种是使用动态规划,我们使用辅助数组来存储回文情况,如果在判断P(i, j)时,p(i+1, j-1)时是回文,我们就不用在进行判断,只需要判断S[i] == s[j ]即可。从而减少时间复杂为O(n2), 空间复杂度为O(n2)(空间换时间思路)。

   第三种思路是我们从头开始遍历,然后以此为中心考虑基数个数和偶数个数的时候,得出最长字符串的结果。这样复杂度为O(n2),空间复杂度为O(1)。

图示思路


          

代码解决


 class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
if len(s) < : # 长度小于2直接返回
return s
res_str = ''
for i in range(len(s)): # 从头开始遍历
tem = self.find_par(s, i, i) # 以基数的形式进行查找最长回文 if len(tem) > len(res_str): # 比较大小
res_str = tem tem = self.find_par(s, i, i+) # 以偶数形式进行查找
if len(tem) > len(res_str):
res_str = tem
return res_str # 返回结果 def find_par(self, s, start, end): # 查找函数
while start >= and end < len(s) and s[start] == s[end]: # 以当前小标为基准,前后移动,知道不相等或者前后超出范围为止。
start -=
end +=
return s[start+: end] # 返回结果字符串

  

【LeetCode每天一题】Longest Palindromic Substring(最长回文字串)的更多相关文章

  1. 5. Longest Palindromic Substring -- 最长回文字串

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

  2. Leetcode5.Longest Palindromic Substring最长回文字串

    给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &quo ...

  3. [LeetCode] Longest Palindromic Substring 最长回文串

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

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

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

  5. leetcode 第五题 Longest Palindromic Substring (java)

    Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...

  6. leetcode第五题--Longest Palindromic Substring

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

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

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

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

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

  9. 【LeetCode】5. Longest Palindromic Substring 最长回文子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...

随机推荐

  1. @ControllerAdvice注解的使用

    package com.vcredit.ddcash.monitor.controller; import com.vcredit.ddcash.monitor.model.dto.Response; ...

  2. VBS数组导出到Excel

    <script language="vbscript"> dim arr(9999,4) for i=0 to 9999 for j = 0 to 4 arr(i,j) ...

  3. 用C# 7.0的switch...case模式匹配取代一堆if语句

    今天在重构代码时对下面的一堆if语句实在看着不顺眼. if(activation == null) { _logger.LogError("x1"); return Boolean ...

  4. 使用ASP.NET Core的User Secrets特性

    昨天在一个集成测试项目中实际使用 ASP.NET Core 的 user secrets 保存敏感配置信息,避免了直接保存在 appsettings.json 中,在这篇随笔中记录一下. 使用 use ...

  5. MQTT 单片机端讲解

    有空了和大家分享一下,如何从头架构一个高效mqtt并行客户端,基于传统GPRS等较差网络环境和网关等网络环境好的情景(当然仔细讲解mqtt的基本函数使很有必要的).---这会正忙着搬砖 MQTt协议 ...

  6. 解决ubuntu系统“XXX is not in the sudoers file”错误

    用adduser新建的用户,当时只输入了一个用户名,没做其它操作,这样就建立了一个normal用户.在执行sudo vim hadoop-env.sh时,报“*** is not in the sud ...

  7. MySQL5.6启动报错The server quit without updating PID file

    Mysql启动报错如下: [root@db01 opt]# service mysqld start Starting MySQL.... ERROR! The server quit without ...

  8. webstorm 智能提示忽略大小写

    setting-Editor-General-Code Completion里的 Case sensitive completion: 可以设置只第一个字母敏感.完全敏感或者不敏感. 选择none.. ...

  9. CORS跨域-Nginx使用方法(Access-Control-Allow-Origin错误提示)

    问题说明 当出现上图这个的时候,是访问请求外域URL无法访问,浏览器认为访问外域URL不安全,导致访问不了简称跨域问题.而这上面出现一句很重要的话“NO Access-Control-Allow-Or ...

  10. [skill][gdb][coredump][abrt] 使用abrt管理程序coredump

    abrt:Automatic bug detection and reporting tool https://github.com/abrt/abrt 常用的命令: abrt-auto-report ...