【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 ...
随机推荐
- The influence of informal governance mechanisms on knowledge integration
Title:The influence of informal governance mechanisms on knowledge integration within cross-function ...
- 关于web上的图片格式问题,新的彩蛋
我们耳熟能详的几种格式无外乎 png-8,png-24,jpeg,gif,svg. 但是上面的那些都不是能够另人惊喜的答案.关于新技术的是Webp,Apng.(是否有关注新技术,新鲜事物) 1)Web ...
- 在SQL脚本中的注释引起的奇怪问题
在数据库安装包中,我们通过osql.exe这个工具来对相关的数据库脚本进行更新,昨天突然发现安装包报错了,说脚本错误,但我们将脚本拿到数据库查询分析器中执行,一切OK. 问题出在哪里呢? 通过使用os ...
- OpenCV3读取、写入和保存图像
需要说明的是在OpenCV3中已经将imread()和imwrite()函数转移到imgcodecs模块中,因此读写图像时,需要包含imgcodecs.hpp头文件,但是highgui.hpp头文件中 ...
- Qt模拟C#的File类对文件进行操作
其实只是QT菜鸟为了练习而搞出来的 文件头: #include <QFile> #include <QString> #include <iostream> #in ...
- c#中const与readonly区别
const 的概念就是一个包含不能修改的值的变量.常数表达式是在编译时可被完全计算的表达式.因此不能从一个变量中提取的值来初始化常量.如果 const int a = b+1;b是一个变量,显然不能再 ...
- jquery 点击查看,收起特效
<div class="all"> <p><a href="javascript:;" id="onvk"&g ...
- jquery nicescroll 配置参数
jQuery滚动条插件兼容ie6+.手机.ipad http://www.areaaperta.com/nicescroll/ jQuery(function($){ $("#scrollI ...
- 使用GruntJS构建Web程序
Gruntjs是JavaScript项目的构建工具,也是基于node的一个命令行工具.很多开源JS项目都是使用它搭建.如jQuery.Qunit.CanJS等.它有以下作用 合并JS文件 压缩JS文件 ...
- php获取数组中重复数据的两种方法
分享下php获取数组中重复数据的两种方法. 1,利用php提供的函数,array_unique和array_diff_assoc来实现 <?php function FetchRepeatMem ...