24-Longest Palindromic Substring-Leetcode
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.
这里给出一种AC的动态规划解法:时间和空间复杂度O(n2)
class Solution {
public:
string longestPalindrome(string s) {
const int n=s.size();
bool f[n][n];
fill_n(&f[0][0],n*n,false);
size_t max_len = 1,start =0;
for(size_t i =0;i<s.size();++i){
f[i][i]=true;
for(size_t j=0;j<i;++j){
f[j][i]=(s[j]==s[i]&&(i-j<2||f[j+1][i-1]));
if(f[j][i]&&max_len<(i-j+1))
{
max_len = i-j+1;
start = j;
}
}
}
return s.substr(start,max_len);
}
};
24-Longest Palindromic Substring-Leetcode的更多相关文章
- Longest Palindromic Substring——LeetCode
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- Longest Palindromic Substring -LeetCode
题目 Given a string s,find the longest palindromic substring in S.You may assume that the maximum len ...
- Longest Palindromic Substring leetcode java
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- [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 5. Longest Palindromic Substring
写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...
- LeetCode 5 Longest Palindromic Substring(最长子序列)
题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...
- 【JAVA、C++】LeetCode 005 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 ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
随机推荐
- hystrix的配置说明
在我们的日常开发中,有些时候需要和第三方系统进行对接操作,或者调用其他系统的 api 接口,但是我们不能保证这些第三方系统的接口一定是稳定的,当系统中产生大量的流量来访问这些第三方接口,这些第三方系统 ...
- poj 1704 Georgia and Bob (nim)
题意: N个棋子,位置分别是p[1]...p[N]. Georgia和Bob轮流,每人每次可选择其中一个棋子向左移动若干个位置(不能超过前一个棋子,不能超出最左边[位置1]且不能不移) Georgia ...
- ARM 链接配置.lds文件学习<转>
本文由Jacky原创,来自http://blog.chinaunix.net/u1/58780/showart.php?id=462971 对于.lds文件,它定义了整个程序编译之后的连接过程,决定了 ...
- linux 内核源代码情景分析——用户堆栈的扩展
上一节中,我们浏览了一次因越界访问而造成映射失败从而引起进程流产的过程,不过有时候,越界访问时正常的.现在我们就来看看当用户堆栈过小,但是因越界访问而"因祸得福"得以伸展的情景. ...
- Vue脚手架最新版本安装使用
现在很多的插件如Vant 这类的样式框架,都去兼容了Vue的3.0版本,所以我总结一下如何去简单的搭建一个Vue3.0的框架 开始 一,如何安装 在这里说明一下,Vue脚手架版本,和Vue版本是两个东 ...
- #ifndef #define #endif #ifdef 避免重复引用
一:在什么阶段处理 ? 预处理 预处理 预处理 首先注意这四个头文件保护符是在预处理阶段由系统默认的预处理器(Linux操作系统上默认是cpp)来处理的.它们的含义如下: #define XXX // ...
- Vmware 中 Kali linux 2020 设置共享文件夹
前言 kali2020已经自带vmware-tools工具,因此,只要是原装的kali2020是不需要继续安装vmhgfs工具的. 过程 vmware 设置共享目录 使用vmware-hgfsclie ...
- vue禁用浏览器回退
解决方案 mounted() { history.pushState(null, null, document.URL) window.addEventListener('popstate', () ...
- R语言中文分词包jiebaR
R语言中文分词包jiebaR R的极客理想系列文章,涵盖了R的思想,使用,工具,创新等的一系列要点,以我个人的学习和体验去诠释R的强大. R语言作为统计学一门语言,一直在小众领域闪耀着光芒.直到大数据 ...
- Django 小实例S1 简易学生选课管理系统 11 学生课程业务实现
Django 小实例S1 简易学生选课管理系统 第11节--学生课程业务实现 点击查看教程总目录 作者自我介绍:b站小UP主,时常直播编程+红警三,python1对1辅导老师. 课程模块中,学生需要拥 ...