leetcode 最长回文子串
2019-06-20 16:12
369 查看
题目:
给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
示例 1:
输入: "babad"
输出: "bab"
注意: "aba" 也是一个有效答案。
示例 2:
输入: "cbbd"
输出: "bb"
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-palindromic-substring
思路:
由题目可知回文子串一定从中间开始,两边相等;
任意字符向两边扩展,若是头尾相等,则最长回文子串是子串加上头尾;
若头尾不想等,则是去头的部分的最长回文子串或者去尾的部分的最长回文子串。
[code]def longestPalindrome(self, s: str) -> str: if not s: return '' size=len(s) start,maxlen=0,0 for i in range(size): if i-maxlen>=1 and s[i-maxlen-1:i+1]==s[i-maxlen-1:i+1][::-1]: start=i-maxlen-1 maxlen+=2 continue if i-maxlen>=0 and s[i-maxlen:i+1]==s[i-maxlen:i+1][::-1]: start=i-maxlen maxlen+=1 return s[start:start+maxlen]
参考文献:https://blog.csdn.net/asd136912/article/details/78987624
相关文章推荐
- [LeetCode]—Longest Palindromic Substring 最长回文子串
- leetcode--最长回文子串--python
- LeetCode-5 最长回文子串
- leetcode 5. 最长回文子串(Java版)
- 2014再见 2015你好 【附带】[LeetCode]最长回文子串
- LeetCode5. Longest Palindromic Substring(最长回文子串:Manacher算法)
- leetcode:最长回文子串
- LeetCode_5_Longest Palindromic Substring_最长回文子串
- leetcode第五题——最长回文子串
- Longest Palindromic Substring [LeetCode] 最长回文子串
- LeetCode 5 Longest Palindromic Substring(最长回文子串,暴力剪枝/DP/曼彻斯特算法)
- LeetCode5. Longest Palindromic Substring 最长回文子串 4种方法
- LeetCode题库解答与分析——#5.最长回文子串LongestPalindromicSubstring
- 一天一道LeetCode(三)5. 最长回文子串
- leetcode-5. Longest Palindromic Substring(寻找最长回文子串)
- 【leetcode每日一题】【2019-04-16】5.最长回文子串
- Leetcode题解1:最长回文子串 JAVA
- LeetCodeOJ_5_m_Longest Palindromic Substring(最长回文子串)
- LeetCode5. 最长回文子串(Java)
- LeetCode No.5. 最长回文子串