LeetCode-005 Longest Palindromic Substrin

【题目】

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最长为1000,保证有唯一解

【思路】

原字符串用特殊字符#间隔,如下所示:

#a#b#c#b#d#

从字符串的第二个字符a开始, 依次搜索以当前字符串为中心的最大回文字符串半径,直到到最后第二个字符d结束

注意此字符串中找出的回文肯定是以#作为起始和结尾的。

【代码】

class Solution {
public:

    string longestPalindrome(string s) {
        string news = "";
        for(int i=0; i<s.length(); i++){
            news += "#";
            news += s[i];
        }
        news += "#";

        int maxStart = -1;  //最大回文起始位置
        int maxEnd = -1;    //最大回文结束位置
        int maxRadius = 0;  //最大回文半径
        int maxCenter = 1;  //最大回文中心位置
        int cur = 1; // 回文中心
        while(cur < news.length()-1){
			int radius = 0;
			while(cur-radius>=0 && cur+radius<=news.length()-1 && news[cur-radius]==news[cur+radius]){
				radius++;
			}
			radius--;
			if(radius>maxRadius){
				maxCenter = cur;
				maxRadius = radius;
				maxStart = cur-radius;
				maxEnd = cur+radius;
			}
			cur++;
        }

        // maxStart和maxEnd肯定是指向#的,因此需要转换成原文中对应字符的位置
        maxStart = maxStart/2;
        maxEnd = maxEnd/2-1;

        return s.substr(maxStart, (maxEnd-maxStart+1));
    }
};

【思路2】

DP

构造回文判别矩阵,假设A[i][j]表示S[i~j]的子串是否为回文串

转换方程A[i][j]=A[i+1][j-1] && S[i]==S[j]

然后j-i最大的子串即为解

【代码】【TIME LIMIT EXCEEDED】

/*
    题意是找出字符串S中最长回文子串,S最长为1000,保证有唯一解
    思路:
        DP
        创建回文判别矩阵使用,假设A[i][j]表示S[i~j]的子串是否为回文串
        转换方程A[i][j]=A[i+1][j-1] && S[i]==S[j]

 */
class Solution {
public:
    string longestPalindrome(string s) {
        int len = s.length();
        if(len==0)return "";
        vector<vector<bool> > isPal(len, vector<bool>(len, false));
        int maxSubStrLen=0;
        int maxSubStrStart=0;
        //初始化斜对角线,即isPal[i][i], 单个字符肯定是回文
        for(int i=0; i<len; i++){
            isPal[i][i]=true;
            if(1>maxSubStrLen){maxSubStrLen=1; maxSubStrStart=i;}
        }
        //初始化相邻字符构成的子串是否是回文
        for(int i=0; i<len-1; i++){
            isPal[i][i+1]=s[i]==s[i+1];
            if(isPal[i][i+1] && 2>maxSubStrLen){maxSubStrLen=2; maxSubStrStart=i;}
        }
        //初始化i<j的区域
        for(int i=len-3; i>=0; i--){
            for(int j=len-1; j>=i+2; j--){
                isPal[i][j]=isPal[i+1][j-1]&&s[i]==s[j];
                if(isPal[i][j] && j-i+1>maxSubStrLen){maxSubStrLen=j-i+1; maxSubStrStart=i; }
            }
        }
        return s.substr(maxSubStrStart, maxSubStrLen);
    }
};

LeetCode-005 Longest Palindromic Substrin

时间: 2024-12-18 07:45:50

LeetCode-005 Longest Palindromic Substrin的相关文章

Java for 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. 解题思路一: 暴力枚举 共N^2量级个子串(从下标零开始),每次检查需一个for循环,等于是3重for循环,时间复杂度O(n^3) 解题思路二: 动态

LeetCode 005 Longest Palindromic Substring - Java

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: "aba" is also a valid answer. Example: Input: "cbbd" Ou

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

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 005.Longest_Palindromic_Substring (Medium) 链接: 题目:https://oj.leetcode.com/problems/Longest-Palindromic-Substring/ 代码(github):https://github.com/illuz/leetcode

【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 #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

LeetCode-Algorithms #005 Longest Palindromic Substring, Database #179 Consecutive Numbers

LeetCode-Algorithms #005 Longest Palindromic Substring 英语学习时间palindromic: [医] 复发的, 再发的 在数学和计算机上,就指回文 这道题目就是找出给定字符串中最长的回文子串, 可以假定原字符串的长度不超过1000 直接遍历来做肯定是不难, 但也可以想见一定是慢得可以. 那么我的另一个想法是, 从原串中的每一个字符, 或每两个字符中间的空隙开始, 向左右两边判断是否为回文串, 最后找出最长的 1 class Solution

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