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 ...
随机推荐
- 问题-Delphi编译到最后Linking时总是出现与ntdll.dll有关的错误还有Fatal Error Out of memory错误
1.跳出错误法 ===================================================在主界面的implementation {$R *.dfm} 下放入以下代码: ...
- 问题-Delphi在多线程中使用其他窗体上的控件,报“尚未调用CoInitialize”解决方法
1.uses ActiveX; 2. procedure HLCJ.Execute;begin CoInitialize(nil); //要使用的控件 CoUninitialize; ...
- A Tour of Go Struct Literals
A struct literal denotes a newly allocated struct value by listing the values of its fields. You can ...
- 使用 Infragistics 的 NetAdvantage 组件时替换部分菜单语言的方法
Infragistics 的 NetAdvantage 组件很好用,不过有些自动的菜单默认都是英文的,一直想替换成中文,以下就是研究了一下午整出来的几行代码,貌似网上很难找到此类的相关资料,替换的资源 ...
- 设置tomcat内存
设置Tomcat启动的初始内存其初始空间(即-Xms)是物理内存的1/64,最大空间(-Xmx)是物理内存的1/4. 可以利用JVM提供的-Xmn -Xms -Xmx等选项可进行设置 三.实例,以下给 ...
- [HTTPS] MAN IN THE MIDDLE (MITM)
If you go a public caffee shop, they have free wifi. How could you make sure your infomration cannot ...
- MVC - 基础
什么是MVC模式 传统的WebForm发展到如今出现不少的缺陷 比如为了解决Http的无状态 WebForm模式使用了ViewState来保存客户端和服务端数据 过量的使用则会造成页面臃肿不堪 大量服 ...
- 常用工具之stunnel
The stunnel program is designed to work as an SSL encryption wrapper between remote client and local ...
- 如何从零开始学习DIV+CSS
CSS是样式,DIV是层.DIV+CSS是网站标准(web标准),通常为了说明与HTML网页设计语言中的表格(table)定位方式的区别.因为XHTML网站设计标准中,不再使用表格定位技术,而是采用D ...
- hdu1753java(大数相加)---BigDecimal类
大明A+B Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...