【leetcode】Longest Palindromic Substring (middle) 经典
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.
动态规划解法 O(n2) 超时
string longestPalindrome(string s) {
if(s.empty()) return s;
vector<vector<bool>> isPalindrome(s.length() + , vector<bool>(s.length() + , false));
string ans = s.substr(,);
for(int i = ; i <= s.length(); i++) //长度
{
for(int j = ; j <= s.length() - i; j++) //起始位置
{
if((i <= ) || (isPalindrome[j + ][i - ] && s[j] == s[j + i - ]))
{
isPalindrome[j][i] = true;
if(i > ans.length())
ans = s.substr(j, i);
}
}
}
return ans;
}
O(N)解法 AC
string longestPalindrome2(string s)
{
if(s.empty()) return s;
string ss = "#";
for(int i = ; i < s.length(); i++)
{
ss += s.substr(i, ) + "#";
}
vector<int> P(ss.length()); //记录新字符串以每一个字符为中心时 包括中心的一半长度 只有一个时长度为1
string ans = s.substr(,);
int mx = ; //向右最远的对称位置+1
int id = ; //mx对应的中心位置
for(int i = ; i < ss.length(); i++)
{
P[i] = (mx > i) ? min(P[*id - i], mx - i) : ;
while(i - P[i] >= && i + P[i] < ss.length() && ss[i - P[i]] == ss[i + P[i]]) P[i]++;
if(P[i] - > ans.length())
ans = s.substr((i - P[i] + )/, P[i] - );
if(i + P[i] > mx)
{
mx = i + P[i];
id = i;
}
}
return ans;
}
【leetcode】Longest Palindromic Substring (middle) 经典的更多相关文章
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- Leetcode Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- [LeetCode] Longest Palindromic Substring(manacher algorithm)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- C++ leetcode Longest Palindromic Substring
明天就要上课了,再过几天又要见班主任汇报项目进程了,什么都没做的我竟然有一种迷之淡定,大概是想体验一波熬夜修仙的快乐了.不管怎么说,每天还是要水一篇博文,写一个LeetCode的题才圆满. 题目:Gi ...
- Leetcode: Longest Palindromic Substring && Summary: Palindrome
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- LeetCode:Longest Palindromic Substring 最长回文子串
题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- Leetcode: Longest Palindromic Substring. java
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- LeetCode——Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- [LeetCode]Longest Palindromic Substring题解(动态规划)
Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...
随机推荐
- ArcMap 10.2 crashes during Loading Document launch stage
问题描述: ArcMap unexpectedly exits during the "Loading Document..." stage on startup. No erro ...
- Oracle 简介 三层结构
引言: 主流数据库:sql server, oracle, my sql,IBM公司的DB2 ,oracle占有量很大 dbms(database management system)数据库管理系统 ...
- iOS开发 中的代理实现
iOS开发 中的代理实现 关于今天为什么要发这篇文字的原因:今天在和同事聊天的时候他跟我说项目中给他的block有时候不太能看的懂,让我尽量用代理写,好吧心累了,那就先从写个代理demo,防止以后他看 ...
- C#判断Textbox是否为数字
第一种方法: try { ) { //操作代码 } else { MessageBox.Show("必须是正整数"); } } catch (FormatException) { ...
- (转)Facebook内部分享:26个高效工作的小技巧
春节假期马上就要结束了,该收收心进入新一年的工作节奏了~分享 26 个高效工作的小技巧,希望对大家有所帮助~(我发现自己只有最后一条执行得很好,并且堪称完美!) 1.时间常有,时间优先. 2.时间总会 ...
- ssh 私匙登录, 文件rswrst权限
skill -KILL -u user1 //注销用户 ssh 免密码登录 http://flysnowxf.iteye.com/blog/1567570 (说是防火墙的问题) http://fly ...
- c#winform,制作可编辑html编辑器
大神勿喷,新手记笔记 材料 网上下载kindeditor,动手在写个htmldome,图中的e.html.然后全部扔到了bin/debug下面,(x86是要扔到bin/x86/debug) 中间bod ...
- 借网上盛传2000w记录介绍多进程处理
2000w的数据在网上搞得沸沸扬扬,作为技术宅的我们也来凑凑热闹.据了解网上有两个版一个是数据库文件另一个是CSV文件的,前者大小有好几个G后者才几百M.对于不是土豪的我们当然下载几百M的.至于在哪下 ...
- 静态页面中如何传json数据
首页传递参数组装成json数据,再编码 var param="{type:'"+type+"',text:'"+select_text+"',sele ...
- Editplus 中将文本换行替换为<p>标签的正则表达式
在Editplus.Notepad++编辑器里文本直接复制到在线编辑器里是不带<p>标签的,只是简单的将换行"\n"替换为"<br />" ...