最长回文子串:

1. 暴力搜索   时间复杂度O(n^3)

2. 动态规划

  • dp[i][j] 表示子串s[i…j]是否是回文
  • 初始化:dp[i][i] = true (0 <= i <= n-1);  dp[i][i-1] = true (1 <= i <= n-1); 其余的初始化为false
  • dp[i][j] = (s[i] == s[j] && dp[i+1][j-1] == true)

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

3.  以某个元素为中心,分别计算偶数长度的回文最大长度和奇数长度的回文最大长度。时间复杂度O(n^2),空间O(1)

4. Manacher算法,时间复杂度O(n), 空间复杂度O(n)。 具体参考如下链接:

    http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html

  

class Solution {
public:
string longestPalindrome(string s) {
int n = *s.length() + ;
char cstr[n];
cstr[] = '\1';
cstr[n-] = '\0';
for(int i = ; i < n; i += )
cstr[i] = '#';
for(int i = ; i < n; i += )
cstr[i] = s[i/ - ];
int *p;
p = new int[n];
memset(p, , sizeof(int)*n); int mx, id, i, j;
for (id = , i = , mx = ; i < n; ++i) {
j = *id - i;
p[i] = (mx > i) ? min(p[j], mx-i) : ;
while (cstr[i + p[i] + ] == cstr[i - (p[i] + )])
++p[i];
if (i + p[i] > mx){
id = i;
mx = id + p[id];
}
} int max = -;
for (i = ; i < n-; ++i) {
if (max < p[i]) {
max = p[i];
id = i;
}
}
return s.substr( (id - max - )/ , max);
}
private: };

  

leetcode problem (5) Longest Palindromic Substring的更多相关文章

  1. 【一天一道LeetCode】#5 Longest Palindromic Substring

    一天一道LeetCode系列 (一)题目 Given a string S, find the longest palindromic substring in S. You may assume t ...

  2. 【LeetCode OJ】Longest Palindromic Substring

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

  3. LeetCode(3)题解: Longest Palindromic Substring

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

  4. 【LeetCode】5. Longest Palindromic Substring 最长回文子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...

  5. 【LeetCode】5. Longest Palindromic Substring 最大回文子串

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

  6. 【leetcode】5. Longest Palindromic Substring

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

  7. leetcode题解 5. Longest Palindromic Substring

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

  8. LeetCode OJ:Longest Palindromic Substring(最长的回文字串)

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

  9. 【LeetCode】005. Longest Palindromic Substring

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

随机推荐

  1. CodeForces 540E - Gerald and Giant Chess(数论)

    给一个棋盘,需要从左上角走到右下角,有部分点不能走,求一共有多少种走法. 首先要知道从一个点A到另一个点B在没有障碍下有多少种走法.保证A在B的左上方,如图 一共需要走(X+Y)步(图中△x,△y), ...

  2. 国外一些好用的UX/UI设计工具和资源分享

    国外一些好用的UX/UI设计工具和资源分享 你今天使用的设计工具也许不再适合以后的网页和APP设计项目了.新的工具不断的推出市场,目标只有一个,让你的工作更快.更容易而且工作成效更好.今天分享的这些U ...

  3. context-param和init-param区别

    转载 http://www.cnblogs.com/hzj-/articles/1689836.html <context-param>的作用:web.xml的配置中<context ...

  4. 从div盒子模型谈如何写可维护的css代码(转)

    市面上我们常常会看到各种各样的设计模式书籍,Java设计模式.C#设计模式.Ruby设计模式等等.在众多的语言设计模式中我唯独找不到关于CSS设计模式的资料,即使在网上找到类似内容,细细一看之下才发觉 ...

  5. 标准I/O库之打开和关闭流

    下列三个函数打开一个标准I/O流. #include <stdio.h> FILE *fopen( const char *restrict pathname, const char *r ...

  6. Android EventBus

    https://github.com/greenrobot/EventBus onEvent:如果使用onEvent作为订阅函数,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也 ...

  7. 使用HttpURLConnection实现在android客户端和服务器之间传递对象

    一般情况下,客户端和服务端的数据交互都是使用json和XML,相比于XML,json更加轻量级,并且省流量,但是,无论我们用json还是用xml,都需要我们先将数据封装成json字符串或者是一个xml ...

  8. arcgis 获得路径和环境变量信息

    import arcpy import sysimport osimport string reload(sys) sys.setdefaultencoding("utf8")sc ...

  9. arcgis python 获得所有的工具名称

    #######################import  arcgisscripting import  string; gp = arcgisscripting.create(9.3); ### ...

  10. Java并发——同步容器与并发容器

    同步容器类 早期版本的JDK提供的同步容器类为Vector和Hashtable,JDK1.2 提供了Collections.synchronizedXxx等工程方法,将普通的容器继续包装.对每个共有方 ...