题意:

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.  (Medium)

分析:

采用动态规划,dp[i][j]表示从s[i]到s[j]的字串是否回文,然后按照字串长度进行遍历,如果s[i] == s[j] && dp[i + 1][j - 1] == 1,则dp[i][j] = 1

并更新起始位置和最大长度。

初始化除了要将dp[i][i] i=0,1,2...n-1初始化外,还需要初始化dp[i][i+1]

最终得到维护好的最大字串起始位置和长度,返回该字串。

代码:

 class Solution {
public:
string longestPalindrome(string s) {
int dp[][] = {};
int start = , length = ;
for (int i = ; i < s.size(); ++i) {
dp[i][i] = ;
}
for (int i = ; i < s.size() - ; ++i) {
if (s[i] == s[i + ]) {
dp[i][i + ] = ;
start = i;
length = ;
}
}
for (int k = ; k < s.size(); ++k) {
for (int i = ; i < s.size() - k; ++i) {
if (s[i] == s[i + k] && dp[i + ][i + k - ] == ) {
dp[i][i + k] = ;
start = i;
length = k + ;
}
}
}
return s.substr(start,length);
}
};

时间复杂度 O(n^2), 空间复杂度 O(n^2);

解法2:

考虑不采用dp数组,节省空间复杂度为O(1);

对每个每个节点作为初始位置,利用helper函数,开始向外扩展,如果满足s[left] == s[right], left--; right++继续判定;

直到不满足条件位置或边界,并更新最大长度和初始位置。

注意拓展节点时除了拓展单一节点情况,还需要拓展两个节点开始的情况 (与dp的初始化条件类似);

代码:

 class Solution {
private:
int helper(const string& s, int left, int right) {
int length = ;
while (left >= && right < s.size() && s[left] == s[right]) {
length = right - left + ;
left--;
right++;
}
return length;
}
public:
string longestPalindrome(string s) {
int start = , length = ;
for (int i = ; i < s.size() ; ++i) {
int l1 = helper(s, i, i);
if (l1 > length) {
length = l1;
start = i - (length / );
}
}
for (int i = ; i < s.size() - ; ++i) {
int l2 = helper(s, i, i+);
if (l2 > length) {
length = l2;
start = i - (length - ) / ;
}
}
return s.substr(start, length);
}
};

LeetCode5 Longest Palindromic Substring的更多相关文章

  1. leetcode--5. Longest Palindromic Substring

    题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

  2. LeetCode-5:Longest Palindromic Substring(最长回文子字符串)

    描述:给一个字符串s,查找它的最长的回文子串.s的长度不超过1000. Input: "babad" Output: "bab" Note: "aba ...

  3. LeetCode5. Longest Palindromic Substring 最长回文子串 4种方法

    题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题意很简单,就是求一个字符串得最长子串,这里的子串指连续的. 本文给 ...

  4. Leetcode5.Longest Palindromic Substring最长回文字串

    给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &quo ...

  5. Leetcode5:Longest Palindromic Substring@Python

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  6. LeetCode5:Longest Palindromic Substring

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  7. [Swift]LeetCode5. 最长回文子串 | Longest Palindromic Substring

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  8. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  9. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

随机推荐

  1. air for ios

    在 Adobe AIR 中为不同屏幕尺寸的多种设备提供支持 使用Flash Builder 4.5进行多平台游戏开发 手机屏幕触控技术与提升AIR在Android上的触控体验 AIR Native E ...

  2. 轻松学习Linux之如何创建可执行脚本

    本文出自 "李晨光原创技术博客" 博客,谢绝转载!

  3. Spark RDD概念学习系列之RDD是什么?(四)

       RDD是什么? 通俗地理解,RDD可以被抽象地理解为一个大的数组(Array),但是这个数组是分布在集群上的.详细见  Spark的数据存储 Spark的核心数据模型是RDD,但RDD是个抽象类 ...

  4. Leveldb Advanced

    [Slice] The return value of the it->key() and it->value() is a simple structure that contains ...

  5. HDU 4588 Count The Carries (数学,计数)

    题意:给定两个十进制数,求二进制中,从x加到y的二进制进了多少位. 析:把这些数字的二进制纵向罗列出来,然后一位一位的把和加起来,最终得到总的进位数.从1到x,第i位上1的总数是x左移i+1位再右移i ...

  6. UVaLive 7500 Boxes and Balls (数学)

    题意:给定 n 个球,每次从每篮子里拿出来一个放在一个新篮子里,并移除相同的,按球的个数进行排序,问你用最多几个球能完成循环. 析:数学问题,很容易发现前n项和就是最多的球数,所以我们只要找最大的n项 ...

  7. iOS设置导航栏样式(UINavigationController)

    //设置导航栏baritem和返回baiitem样式 UIBarButtonItem *barItem = [UIBarButtonItem appearance]; //去掉返回按钮上的字 [bar ...

  8. MVC路由中routes.IgnoreRoute("{resource}.axd/{*pathInfo}") 到底什么意思!

    转自:http://blog.csdn.net/lvjin110/article/details/24638913 参考(1) http://www.cnblogs.com/flyfish2012/a ...

  9. cocos2d-x如何截屏并保存图片

    转自:http://blog.csdn.net/wolfking_2009/article/details/11022693 static void ScreenShoot() { CCSize si ...

  10. Pgpool烂泥扶不上墙

    写这篇文章,是想好心地给打算使用Pgpool的人提个醒: Pgpool 真的不适合在企业范围使用. 我的主要理由是: 设计陈旧: 一旦后台任何节点Down掉,都会引发failover,它会杀掉所有子进 ...