5. Longest Palindromic Substring

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.

思路:(beat 2.63%)

  以一个位置为准,考察两侧相同距离是否相同。这样会导致"aaaaaaaaaaaaaaaaaaaaaaaaaa..."达到最坏O(n2)的时间复杂度。

  而且这种情况下需要对两种回文类型分开提取。

代码 C++:

class Solution {
public:
    string longestPalindrome(string s) {
        if (s.length() <= 1)
            return s;
        string solve = "";

        for (int flag = 0; flag < s.length() - solve.length() / 2; flag++) {
            string temp = "";
            temp.push_back(s[flag]);
            for (int i = flag - 1,j = flag + 1; i >=0 && j < s.length() && s[i] == s[j]; i--,j++) {
                string ins = "";
                ins.push_back(s[i]);
                temp.insert(0, ins);
                temp.push_back(s[i]);
            }
            if (temp.length() > solve.length())
                solve = temp;
        }
        for (int flag = 0; flag < s.length() - solve.length() / 2; flag++) {
            string temp = "";
            for (int i = flag,j = flag + 1; i >=0 && j < s.length() && s[i] == s[j]; i--,j++) {
                string ins = "";
                ins.push_back(s[i]);
                temp.insert(0, ins);
                temp.push_back(s[i]);
            }
            if (temp.length() > solve.length())
                solve = temp;
        }
        return solve;
    }
};

Reference: https://leetcode.com/discuss/32204/simple-c-solution-8ms-13-lines

思路:

用i遍历字符串,j,k初始化与i相等,k++ if 相等,这样,j,k就把中奖相同的给框住了,然后再j--k++找到相同,用min_pos记录回文串首位置,max_length记录回文串长度,即j-k+1。

Θ(1)存储空间,将两种回文串一同解决,且没有上述O(n2)的最坏情况

代码:

string longestPalindrome(string s) {
    if (s.empty()) return "";
    if (s.size() == 1) return s;
    int min_start = 0, max_len = 1;
    for (int i = 0; i < s.size();) {
      if (s.size() - i <= max_len / 2) break;
      int j = i, k = i;
      while (k < s.size()-1 && s[k+1] == s[k]) ++k; // Skip duplicate characters.
      i = k+1;
      while (k < s.size()-1 && j > 0 && s[k + 1] == s[j - 1]) { ++k; --j; } // Expand.
      int new_len = k - j + 1;
      if (new_len > max_len) { min_start = j; max_len = new_len; }
    }
    return s.substr(min_start, max_len);
}
时间: 2024-10-27 04:07:28

5. Longest Palindromic Substring的相关文章

[string]Longest Palindromic Substring

Total Accepted: 82026 Total Submissions: 379898 Difficulty: Medium 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. Subscrib

LeetCode OJ:Longest Palindromic Substring(最长的回文字串)

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. 玩了两天dota2,罪过罪过,还是应该老老实实刷题啊. 题目求得是最长的回文子串,这里使用的是暴力的解法,也就是解决两种回文"asdsa"以

5. Longest Palindromic Substring - Unsolved

https://leetcode.com/problems/longest-palindromic-substring/#/description Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example: Input: "babad" Output: "bab" Note: &

[Leetcode] Longest palindromic substring 最长回文子串

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. 做这道题之前要先了解什么是回文子串.回文串通俗的解释是,分别从字符串两端开始遍历,得到的结果相同,如"abba",从两端的遍历结果都是:&q

【leedcode】 Longest Palindromic Substring

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. https://leetcode.com/problems/longest-palindromic-substring/ 求最大回文的长度,其实这道题

LeetCode #5 Longest Palindromic Substring (M)

[Problem] 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. [Analysis] 这题的思路有很多种,网上也有各种讨论.这里我采用的是个人觉得比较好理解的一种利用Dynamic Progra

LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法

LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之余全部耗在这上面了,只怪自己基础差.本文主要介绍使用Manacher线性算法来求解字符串的最长回文子字符串. 题目 Given a string S, find the longest palindromic substring in S. You may assume that the maxim

[C++]LeetCode: 99 Longest Palindromic Substring (最长回文子串)

题目: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. 思路:题目要求的s的一个最长回文子串.暴力解决办法就是枚举所有的子串,再对每个子串进行回文判断.进行剪枝,我们考虑可以使用动态规划来避免重复的判

[译]最长回文子串(Longest Palindromic Substring) Part II

[译+改]最长回文子串(Longest Palindromic Substring) Part II 问题:给定字符串S,求S中的最长回文子串. 在上一篇,我们给出了4种算法,其中包括一个O(N2)时间O(1)空间的算法(中心检测法),已经很不错了.本篇将讨论一个O(N)时间O(N)空间的算法,即著名的Manacher算法,并详细说明其时间复杂度为何是O(N). 提示 +BIT祝威+悄悄在此留下版了个权的信息说: 先想想有什么办法能改进中心检测法. 考虑一下最坏的情况.★ 最坏的情况就是各个回文