LeetCode #5 Longest Palindrome

Description



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

Sample


Input: "babad"

Output: "bab"

Note: "aba" is also a valid answer.
Input: "cbbd"

Output: "bb"

思路

  回文串是正着读、反着读都一样的串,比如 "a"、"level"、"noon"。

  解本道题最无脑的办法肯定是 O(n^3) ,这里不讨论了。

  我想到了一个 O(n^2) 的办法,由于回文串都是对称的,那么它的中点就十分关键,我们维护一个指针 i 指向回文串的中点,维护一个指针 j 确定回文串的长度,判断序列 S[ i-j .. i ] 与序列 S[ i .. i+j ] 的逆序列S‘ 是否相同,如果相同,让 j++。

  举个例子,如 abcba ,当 i = 2 (S[2] = c) 时,j 赋值为 1,使得 s[ i-1..i ] 与 s[ i..i+1 ] 的逆序列分别为 [bc]、[bc] ,两者是相同的,那么就让 j++ 以表示回文串的长度增加,之后再比较 S[i-2..i] 与 s[i..i+2] 的逆序列是否相同...

  还需要注意回文串的长度分奇偶,比如 "bb"、"bab" ,敲代码的时候也要分别对两种情况进行修改。

  我用 map 去优化这道题存储回文串的运行时间,红黑树实现的map 插入效率为 O(lgn) ,但是算法时间的上界主要受指针 i j 的影响,,所以算法的时间复杂度为 O(n^2 ) ,这儿我想玩玩 map 的比较函数,所以时间会久一点。实际上,用两个变量存储最大回文串的长度与最长回文串就好了。  

#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
class Solution {
public:
    string longestPalindrome(string s) {
        int len = 0, maxlen = 0;
        std::map<int, string, greater<int> > m; //use greater<> sort key instead of default less<>
        string substr = "";
        for (int i = 0; i < s.size(); ++i) {
            findPal(s, i-1, i+1, len, substr); // Palindrome is adjacent
            if (maxlen < len) {
                maxlen = len;
                m[len] = substr;
            }
            findPal(s, i, i+1, len, substr); // Palindrome is not adjacent
            if (maxlen < len) {
                maxlen = len;
                m[len] = substr;
            }
        }
        substr = m.begin()->second; //the first value means longestPalindrome
        return substr;
    }
private:
    void findPal(const string& s, int left, int right, int& len, string& substring) {
        while (left >= 0 && right <= s.size()-1 && s[left] == s[right]) {
            left--;
            right++;
        }
        len = right -left -1; //subtract extra two lengths
        if (len < 1)
            return; //length of Palindrome must >= 1
        else
            substring = s.substr(left+1, len);
    }
};

  其实还有一种办法可以更快,那就是采用 区间DP 的 manacher 算法。但是我没悟到,之后理解了会再更新这篇博客。

  

原文地址:https://www.cnblogs.com/Bw98blogs/p/8376689.html

时间: 2024-09-30 20:40:12

LeetCode #5 Longest Palindrome的相关文章

LeetCode 409 Longest Palindrome

Problem: Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here. Assume the l

LeetCode——409. Longest Palindrome

题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here. Note: Assume the

LeetCode the longest palindrome substring

回文检测,参考http://blog.csdn.net/feliciafay/article/details/16984031 使用时间复杂度和空间复杂度相对较低的动态规划法来检测,具体的做法 图一 偶数个回文字符情况 图二 奇数个回文字符情况 核心就是如果一个子串是回文,如果分别向回文左右侧扩展一个字符相同,那么回文就向外扩展一位. 实现的代码如下 bool table[1000][1000] = {false}; int sStart = 0; int iLength = 1; int n

Leetcode 之 Longest Palindrome

#include <cstdio> #include <algorithm> int MAX_SIZE = 100000; int gen_odd_str(const char * str, char * odd_str) { int i = 0; for (i = 0; *str != 0 ; ++i, ++str) { odd_str[2 * i] = '#'; odd_str[2 * i + 1] = *str; } odd_str[2 * i] = '#'; odd_str

LeetCode Longest Palindrome

原题链接在这里:https://leetcode.com/problems/longest-palindrome/ 题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa&qu

[LeetCode] Longest Palindrome 最长回文串

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here. Note: Assume the leng

[LeetCode&amp;Python] Problem 409. Longest Palindrome

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here. Note:Assume the lengt

409. Longest Palindrome

题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here. Note:Assume the l

[LeetCode] 125. Valid Palindrome 有效回文

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider that th