LeetCode No.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.

最长回文子串。

有两种思路:

1,从左向右找对称点,从对称点(或者两个相同的相邻字符)向两边搜索,记下搜到的最大长度。

2,设置一个二维bool数组,A[i, j] = TRUE 表示字符串中从i到j是回文串。

那么首先把所有A[i, i]设为TRUE(即长度为1,是回文),再线性找一遍看是否有相邻的相同字符(长度为2的回文),如有将A[i, i + 1]设为TRUE。

从长度为3开始,判断头尾,若头尾相同且A[i+1, j-1] == TRUE(即剩下的是回文),那么这个子串就是回文子串。

我采用的第二种方法。代码如下。

bool tab[1000][1000];

    string longestPalindrome(string s) {
        string max;

        int n = s.size();

        if (n == 0 || n == 1)
            return s;

        for (int i = 0; i < n; i++)
            tab[i][i] = true;

        for (int i = 0; i < n - 1; i++) {
            if (s[i] == s[i + 1]) {
                max = s.substr(i, 2);
                tab[i][i + 1] = true;
            }
        }

        for (int k = 3; k <= n; k++) {
            for (int i = 0; i < n - k + 1; i++) {
                int j = i + k - 1;

                if (tab[i + 1][j - 1] && s[i] == s[j]) {
                    tab[i][j] = true;

                    if (k > max.size())
                        max = s.substr(i, k);
                }
            }
        }

        return max;
    }

两种方法如果我没算错应该都是平方级别的复杂度。

不贴运行时间了,因为同样的代码提交两次时间差很多,没法说明问题。不过这个思路还是值得记录的。

时间: 2024-10-09 19:08:31

LeetCode No.5 Longest Palindromic Substring的相关文章

【LeetCode OJ】Longest Palindromic Substring

题目链接:https://leetcode.com/problems/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. 解题思路:p

LeetCode(3)题解: Longest Palindromic Substring

https://leetcode.com/problems/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. 思路: 我的思路是遍

【LeetCode】005 Longest Palindromic Substring

题目:LeetCode 005 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,找到S中的最长回文子串.假定S最长为100

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"以

【LeetCode】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. 思路:用Manacher算法得到最大回文子串的长度,得到带#的最大回文子串,用split剔除#后转化成String形式输出即可. public

【leetcode】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. 解题分析: 之前百度实习生面试也被问到这个问题.这个题比较简单.就是在分别考虑对称字符字串为奇数和偶数的情况,在不超过字符串范围的情况下向

LeetCode题解 #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. 在给定一个字符串S中,找到最长的回文串. 很恶心的一道题,不过应该都能想到枚举,从第一个开始枚举.枚举的字母向两头延伸,如果相同则继续,不同则开始下一

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