leetcode problem (5) Longest Palindromic Substring
最长回文子串:
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的更多相关文章
- 【一天一道LeetCode】#5 Longest Palindromic Substring
一天一道LeetCode系列 (一)题目 Given a string S, find the longest palindromic substring in S. You may assume t ...
- 【LeetCode OJ】Longest Palindromic Substring
题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- LeetCode(3)题解: Longest Palindromic Substring
https://leetcode.com/problems/longest-palindromic-substring/ 题目: Given a string S, find the longest ...
- 【LeetCode】5. Longest Palindromic Substring 最长回文子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...
- 【LeetCode】5. Longest Palindromic Substring 最大回文子串
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- 【leetcode】5. Longest Palindromic Substring
题目描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- leetcode题解 5. Longest Palindromic Substring
题目: Given a string s, find the longest palindromic substring in s. You may assume that the maximum l ...
- LeetCode OJ:Longest Palindromic Substring(最长的回文字串)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【LeetCode】005. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
随机推荐
- JVM 进行线程同步背后的原理
前言 所有的 Java 程序都会被翻译为包含字节码的 class 文件,字节码是 JVM 的机器语言.这篇文章将阐述 JVM 是如何处理线程同步以及相关的字节码. 线程和共享数据 Java 的一个优点 ...
- Java到底是不是一种纯面向对象语言?
本文由码农网 – Dee1024原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! Java——是否确实的 “纯面向对象”?让我们深入到Java的世界,试图来证实它. 在我刚开始学习 J ...
- PHP面向对象编程
IBM 教程:开始了解 PHP 中的对象 简明现代魔法 一篇很好的入门的Class 文章 - 技术分享 - php教程 少走弯路去学习面向对象编程 -- 简明现代魔法
- Android - TextView Ellipsize属性
Android - TextView Ellipsize属性 本文地址: http://blog.csdn.net/caroline_wendy android:ellipsize属性: If set ...
- SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程 'sys.sp_OACreate' 的访问
sqlserver2008导入excel到数据库的时候报错: SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程 'sys.sp_OACreate' ...
- systemtap 技巧系列 +GDB
http://blog.csdn.net/wangzuxi/article/category/2647871
- I'm back
亲爱的博友们, 请忽略这一条, 这只是我个人的一个记录.
- find——文件查找命令 linux一些常用命令
find 命令eg: 一般文件查找方法: 1. find /home -name file , 在/home目录下查找文件名为file的文件2. find /home -name '*file ...
- Java基础知识强化之IO流笔记37:FileReader/FileWriter(转换流的子类)复制文本文件案例
1. 转换流的简化写法: 由于我们常见的操作都是使用本地默认编码,所以,不用指定编码.而转换流的名称有点长,所以,Java就提供了其子类供我们使用:FileReader / FileWriterOut ...
- Java实现堆排序
import java.util.Scanner; /*堆是一种数据结构,类似于一棵完整的二叉树. * 思想:堆的根节点值最大(最小),将无序序列调整成一个堆,就能找出这个序列的最大值(最小值),将找 ...