【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 ...
随机推荐
- Git sparse checkout
Git的sparse checkout在clone项目仓库时只clone指定路径下的信息. 步骤如下: (1) mkdir yourdir(2) cd yourdir(3) git init(4) g ...
- ios Swift ! and ?
swift ?和!之间区别: Swift 引入的最不一样的可能就是 Optional Value 了.在声明时,我们可以通过在类型后面加一个? 来将变量声明为 Optional 的.如果不是 Opti ...
- 利用js来实现一些常用的算法
示例代码中的arr指的是给出的数组,s指的是数组的起始坐标0,end指的是数组的最后一个坐标arr.length-1,n指的是要查找的数字 查找某个值: 1.线性法 function findInAr ...
- pickle模块的基本使用
pickle是python的biult-in模块: python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储 ...
- OCI的结果输出
绑定变量,把结果以列的方式输出到每一字段输出到一个数组里
- iOS 非ARC基本内存管理系列 -手把手教你ARC——iOS/Mac开发ARC入门和使用(转)
手把手教你ARC——iOS/Mac开发ARC入门和使用 Revolution of Objective-c 本文部分实例取自iOS 5 Toturail一书中关于ARC的教程和公开内容,仅用于技术交流 ...
- 从一个乘法来分析C语言
昨天碰到一个很奇怪的问题,首先来看这段代码: #include<stdio.h> int main(int argc,char *argv[]) { ; ; long long res1 ...
- sqlite3简单使用
下载SQLite3 地址:http://www.sqlite.org/download.html 下载好的文档是SQlite3.exe,假如放在D盘. cmd D: D:\>SQlite3.ex ...
- 【转】 c++拷贝构造函数(深拷贝,浅拷贝)详解
c++拷贝构造函数(深拷贝,浅拷贝)详解 2013-11-05 20:30:29 分类: C/C++ 原文地址:http://blog.chinaunix.net/uid-28977986-id-3 ...
- PHP 归并排序
在我们日常的程序开发时候,有时候需要对一个已知的集合按照一定的规则进行排序,其实当数据的规模不太大时或者数据的有序特征比较明显,其实我们可以采用其它的排序算法例如:Bubble Sort, Inser ...