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 = s.size();

    for(int iLoop = 0; iLoop < n; ++iLoop)
    {
        table[iLoop][iLoop] = true;
    }
    for(int iLoop = 0; iLoop < n-1; ++iLoop)
    {
        if(s[iLoop] == s[iLoop+1])
        {
            table[iLoop][iLoop+1] = true;
            sStart = iLoop;
            iLength = 2;
        }
    }
    for(int  len= 3; len <= n; ++len)
    {
        for(int  i = 0; i < n - len + 1; ++i)
        {
           int  j = len + i -1;
            if(s[i] == s[j] && table[i+1][j-1] )
            {
                table[i][j] = true;
                sStart = i;
                iLength = len;
            }
        }
    }
    return s.substr(sStart, iLength);
时间: 2024-10-11 22:33:31

LeetCode the longest palindrome 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

LeetCode:Longest Palindromic Substring

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. 动态规划解法 T(n) = O(n^2)  ,S(n) = O(n^2); Solutio

5.Longest Palindrome substring

/* * 5.Longest Palindrome substring * 2016-4-9 by Mingyang 自然而然的想到用dp来做 * 刚开始自己做的时候分的条件太细,两个index相等,相差小于3,还有其他 * 但这里这个if写的很好,一个代表了所有为true的情况 * 根本不用管为false的情况,因为自然而然为false * */ public String longestPalindrome(String s) { String res = " "; if (s =

最长回文字串 (The longest palindrome substring)

这两天去学了一下,觉得下面那篇文章写的很好,有例子,比较容易懂,所以转一下. 以下内容来自:hihoCoder: 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一连串的字符串,于是小Hi就向小Ho提出了那个经典的问题:“小Ho,你能不能分别在这些字符串中找到它们每一个的最长回文子串呢?” 小Ho奇怪的问道:“什么叫做最长回文子串呢?” 小Hi回答道:“一个字符串中连续的一段就是这个字符串的子串,而回文

G.Longest Palindrome Substring

链接:https://ac.nowcoder.com/acm/contest/908/G 题意: A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. For example, ”a”.”aba”.“abba” are palindrome and “abc”.”aabb” are not. Let’s d

[LeetCode] Longest Palindrome 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. 面DP题的考官都是神经病. .(吐槽) 貌似挺easy出回文的题,那么今天细致分析下DP的做法!! 以解我被DP问题虐成渣渣的心碎感觉.如有错误请指出

[LeetCode] Longest Palindrome 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. 面DP题的考官都是神经病..(吐槽) 貌似挺容易出回文的题,那么今天仔细分析下DP的做法!!以解我被DP问题虐成渣渣的心碎感觉.如有错误请指出~ 首先

LeetCode 5 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. 动态规划,类似于l