leetcode题解5-Longest Palindromic Substring

标签: leetcode
2人阅读 评论(0) 收藏 举报
分类:

题目:

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(n^2)。

针对此问题有专门的算法-manacher算法,可以实现O(n)复杂度,但是思路还未搞透彻。


代码:

class Solution {
public:
    string longestPalindrome(string s) {
        // O(n^2) 顺序遍历s,以遍历字符为中心双向增长。
        string res;  
        for(int i = 0; i < s.size(); i++){            
            string temp1,temp2; 
            temp1= getPalString(s,i,i);   // 考虑奇数情况 'aba'
            if(temp1.size() > res.size()) res = temp1;
            temp2 = getPalString(s,i,i+1);    // 偶数情况 'abba'
            if(temp2.size() > res.size()) res = temp2;
        }
        return res;
        
    }
    // 函数计算以l,r展开得到的最长对偶串
    string getPalString(string s, int l, int r){
        while(l>=0 && r<s.size() && s[l] == s[r]){
            l--; r++; 
        }
        return s.substr(l+1,r-1-l);   // 返回对偶子串     
    }
};

python


查看评论

LeetCode Longest Palindromic Substring 最长回文子字符串 两种方法分析解答

最难想的地方:P代表一个表,比较难想的就是P表的下标i和j代表原字符串中的两个前后下标s[i]和s[j]的位置。 如果P[i,j]为真,当且仅当si-1,si-2...sj-1,sj这一个子串都为pa...
  • kenden23
  • kenden23
  • 2013-11-23 10:27:57
  • 2936

[LeetCode] 005. Longest Palindromic Substring (Medium) (C++/Java/Python)

[LeetCode] 005. Longest Palindromic Substring (Medium) (C++/Java/Python)
  • hcbbt
  • hcbbt
  • 2015-02-28 00:09:45
  • 3032

LeetCode 5 Longest Palindromic Substring(C,C++,Python,Java)

Problem: Given a string S, find the longest palindromic substring in S. You may assume that the ma...
  • runningtortoises
  • runningtortoises
  • 2015-05-06 15:38:22
  • 1501

LeetCode(5) Longest Palindromic Substring

如果一个字符串从左向右写和从右向左写是一样的,这样的字符串就叫做palindromic string,如aba,或者abba。给定输入一个字符串,要输出一个子串,使得子串是输入字符串的最长的padro...
  • feliciafay
  • feliciafay
  • 2013-11-27 14:00:30
  • 18035

LeetCode第五题,Longest Palindromic Substring

题目原文: Given a string S, find the longest palindromic substring in S. You may assume that the maximu...
  • hu1020935219
  • hu1020935219
  • 2014-08-05 14:29:41
  • 2893

LeetCode5——Longest Palindromic Substring 最易懂的解法

Given a string S, find the longest palindromic substring in S. You may assume that the maximum l...
  • booirror
  • booirror
  • 2016-01-22 23:05:18
  • 1050

[LeetCode][5]Longest Palindromic Substring解析 -Java实现

Q: Given a string S, find the longest palindromic substring in S. You may assume that the maxim...
  • u014629433
  • u014629433
  • 2016-06-05 13:48:00
  • 544

Leetcode 5. Longest Palindromic Substring 新补充Manacher算法

5. Longest Palindromic Substring Total Accepted: 98755 Total Submissions: 434700 Difficulty: Medi...
  • fantasiasango
  • fantasiasango
  • 2016-03-15 09:34:45
  • 618

[LeetCode]5 Longest Palindromic Substring(C++,Python实现)

LeetCode OJ的第五题,如果有问题或者给我指点欢迎来信讨论ms08.shiroh@gmail.com 题目描述...
  • Shiroh_ms08
  • Shiroh_ms08
  • 2014-05-23 21:28:36
  • 3310

[leetcode: Python]5.Longest Palindromic Substring

题目: Given a string s, find the longest palindromic substring in s. You may assume that the maximum ...
  • NXHYD
  • NXHYD
  • 2017-05-17 10:32:45
  • 390
    个人资料
    持之以恒
    等级:
    访问量: 1397
    积分: 212
    排名: 34万+
    文章存档
    最新评论