最长回文子串:

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. hdu3394--Railway(点的双连通分量)

    一个公园中有 n 个景点,景点之间通过无向的道路来连接,如果至少两个环公用一条路,路上的游客就会发生冲突:如果一条路不属于任何的环,这条路就没必要修 问,有多少路不必修,有多少路会发生冲突 每一个连通 ...

  2. Go Slices: usage and internals

    Introduction Go's slice type provides a convenient and efficient means of working with sequences of ...

  3. 用Ajax调用web api,解决URL太长的问题;

    本来是用的WCF,但是服务需要多种方式调用(后台+前端Ajax),最终局面就是我在WCF每个服务中都判断一下↓ #region 解决接收不到Ajax中传来的参数... if (jsonParames ...

  4. Ubuntu 12.04 安装搜狗输入法

    安装指南 Ubuntu / Ubuntu Kylin 14.04 LTS 版本 只需双击下载的 deb 软件包,即可直接安装搜狗输入法. Ubuntu 12.04 LTS 版本 由于 Ubuntu 1 ...

  5. response对象详解

    (响应 javax.servlet.http.HttpServletResponse) 方法名 说明 addCookie 添加一个Cookie对象 addHeader 添加Http文件指定名字头信息 ...

  6. C# LINQ详解(一)

    原文标题:How does it work in C#?-Part 3 (C# LINQ in detail),作者:Mohammand A Rahman. 目录 LINQ 基础 扩展方法-幕后的工作 ...

  7. Wpf OpenFileDialog

                      Microsoft.Win32.OpenFileDialog openFileDialog1 = new Microsoft.Win32.OpenFileDialo ...

  8. Android开源git40个App源码

    http://www.itbbu.com/1039.html (JamsMusicPlayer)很棒的音乐播放器(new)   (F8)日程安排的软件   (Conversations)基于XMPP的 ...

  9. mysql 5.6 oom 图

  10. JSTL-core核心代码标签库中的if,set,out等的功能

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...