LeetCode:5_Longest Palindromic Substring | 最长的回文子串 | 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.

解题思路:
1、简单思路:暴力破解法,时间复杂度O(n^3),肯定通不过。

2、动态规划法:(一般含“最XX”等优化词义的题意味着都可以动态规划求解),时间复杂度O(n^2),空间复杂度O(n^2)。

形如"abba", "abbba"这样的字符串,如果用dp[i][j]表示从下标i到j之间的子字符串所构成的回文子串的长度,则有:

dp[i][j] = dp[i+1][j-1] && s[i] == s[j]

初始化:

奇数个字符:dp[i][i] = 1; 偶数个字符:dp[i][i+1] = 1

 1 //动态规划法
 2 string LongestPalindromicStringDP(string str)
 3 {
 4     size_t n = str.size();
 5     if (n == 0 || n == 1)
 6         return str;
 7     //创建dp二维数组
 8     //d[i][j] = dp[i+1][j-1] && s[i] == s[j]
 9     bool **dp = new bool *[n];
10     for (size_t i = 0; i < n; ++ i)
11         dp[i] = new bool[n];
12
13     int nLongestIndex = 0; //最长回文子串的开始下标
14     int nMaxLen = 0; //长度
15
16     for (size_t i = 0; i < n; i ++)
17         for (size_t j = 0; j < n; j ++)
18             dp[i][j] = false;
19     for (size_t i = 0; i < n; ++ i)
20         dp[i][i] = true; //初始化一个字母
21
22     for (size_t i = 0; i < n - 1; ++ i) {
23         if (str[i] == str[i+1]) {
24             dp[i][i+1] = true; //初始化两个相同的字母"aa"
25             nLongestIndex = i;
26             nMaxLen = 2;
27         }
28     }
29     //从长度3开始操作, (aba)ba, a(bab)a, ab(aba)
30     for (size_t len = 3; len <= n; ++ len) {
31         for (size_t i = 0; i < n-len+1; ++ i) {
32             size_t j = i + len - 1;
33             if (str[i] == str[j] && dp[i+1][j-1]){
34                 dp[i][j] = true;
35                 nLongestIndex = i;
36                 nMaxLen = len;
37             }
38         }
39     }
40
41     //释放dp
42     for (size_t i = 0; i < n; ++ i)
43         delete []dp[i];
44     delete []dp;
45
46     return str.substr(nLongestIndex, nMaxLen);
47 }

3、中心扩散法:时间复杂度O(n^2),空间复杂度O(1)
顾名思义,从一个节点,分别向两边扩散,用两个指针分别向前向后遍历。

 1 //中心扩散法
 2 string LongestPalindromicString(string str)
 3 {
 4     size_t n = str.size();
 5     if (n == 0 || n == 1)
 6         return str;
 7
 8     size_t start = 0;
 9     size_t nMaxLen = 0;
10
11     for (size_t i = 0; i < n; ++ i) {
12         size_t j = i, k = i; //分别从中心向两边扩散
13         while(k < n-1 && str[k] == str[k+1])
14             k++; //有相同字母的情况:"aaa"
15         while(j > 0 && k < n-1 && str[j-1] == str[k+1]){
16             k++; //不同字母情况: "aba"
17             j--;
18         }
19         if(k-j+1 > nMaxLen){
20             nMaxLen = k-j+1;
21             start = j;
22         }
23     }
24     return str.substr(start, nMaxLen);
25 }

4、很明显,这两种方法时间复杂度还是太高了,还有一种算法叫Manacher,时间复杂度能够降为O(n),空间复杂度也为O(n),先记下,以后再做研究吧。

时间: 2024-10-09 20:49:57

LeetCode:5_Longest Palindromic Substring | 最长的回文子串 | Medium的相关文章

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

LeetCode 5_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. 也即:求字符串中最长回文子串. 回文是什么我就不多

manacher算法处理最长的回文子串(二)

在上篇<manacher算法处理最长的回文子串(一)>解释了manacher算法的原理,接着给该算法,该程序在leetcode的最长回文子串中通过.首先manacher算法维护3个变量.一个名为radius[i]的数组,表示以i为中心轴承的回文子串的半径,如abcdcba中,字符d的下标为4,则他的radius[4]=3,下标的为0的a的半径为radius[0]=0,即中心轴不考虑其中.一个idx表示上一次以idx为中心轴的回文.如果当以i为中心的回文在以idx为中心的回文内.则idx不更新,

manacher算法处理最长的回文子串(一)

引言 相信大家都玩过折叠纸张,如果把回文串相当于折叠一个A4纸,比如ABCDDCBA就是沿着中轴线(D与D之间)对折重合,那么这个就是一个回文串.或者是ABCDEDCBA的中轴线就是E,那么沿着中轴线对折也是重合的,所以这个字符串也是一个回文串. 判断一个字符串中的最长回文子串,我们可以对每个字符的两边进行比较,还是如何ABCDEDCBA,在A,B,C,D分别为中心轴向两边扩展的回文子串长度都是1,就是它自己本身,当扫描以E为中心轴时,那么这个回文的最长子串时9,也就是这个字符串本身.然而当用相

转载:LeetCode:5Longest Palindromic Substring 最长回文子串

本文转自:http://www.cnblogs.com/TenosDoIt/p/3675788.html 题目链接 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:暴

[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

[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. http://fisherlei.blogspot.com/2012/12/leetcode-longest-palindromic-substrin

给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。

public String longestPalindrome(String s) { if(s == null||s.length()==0){ return s; } String res = ""; int max=0; boolean[][] dp = new boolean[s.length()][s.length()]; //res=s.substring(0, 1); for(int j = 0;j < s.length();j++){ for(int i=0;i&

找出字符串中的最长的回文子串

1 class Solution { 2 public: 3 string longestPalindrome(string s) { 4 int maxLoc=0; 5 int maxNum=1; 6 const int stringSize=s.size(); 7 if(stringSize==1){ 8 return s; 9 } 10 11 for(int i=1; i!= 2*stringSize-1-1;++i){ 12 int a, b; 13 int num=1; 14 if(i