[LeetCode]Longest Palindromic Substring题解(动态规划)
Longest Palindromic Substring:
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: “babad”
Output: “bab”Note: “aba” is also a valid answer.
Example:
Input: “cbbd”
Output: “bb”
这是一个最长回文子串的问题。解决这个问题有很多种方法,动态规划是其中一种。复杂度为O(n^2)。
用bool矩阵set[i][j]表示子串sub(i,j)是否是回文串,首先初始化:
set[i][i]=true,因为单个字符是回文;
set[i][i+1]=true,if s[i] == s[i+1],因为两个相邻的字母如果相同那么是回文;
然后,如果s[i] == s[j],那么是s[i][j] = s[i+1][j-1];否则s[i][j] = false。
class Solution {
public:
string longestPalindrome(string s) {
bool set[1001][1001]={false};
int len = s.size(),max = 1,start = 0;//最长的回文子串长度是1,从0开始
//下面初始化矩阵
for(int i = 0; i < len; i++){
set[i][i] = true;
if(i<len-1 && s[i]==s[i+1]){
set[i][i+1] = true;
max = 2;
start = i;
}
}
//从长度为3开始,下面的i表示子串长度
for(int i = 3; i <= len; i++){
for(int j = 0; j <= len - i; j++){
if(s[j]==s[j+i-1] && set[j+1][j+i-2]){
set[j][j+i-1] = true;
max = i;
start = j;
}else{
set[j][j+i-1] = false;
}
}
}
return s.substr(start,max);
}
};
[LeetCode]Longest Palindromic Substring题解(动态规划)的更多相关文章
- [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 ...
- 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(manacher algorithm)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 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分析和实现
问题大意是在给定字符串中查找最长的回文子串,所谓的回文就是依据中间位置对称的字符串,比如abba,aba都是回文. 这个问题初一看,非常简单,但是会很快发现那些简单的思路都会带来O(n^3)级别的时间 ...
随机推荐
- flask接收前台的form数据
转自 http://www.cnblogs.com/wanghaonull/p/6340096.html 我主要是想了解 request.form.get('username') 这一部分
- AngularJS源码解析3:RootScope的创建过程
RootScopeProvider简介 RootScopeProvider是angularjs里面比较活跃的一个provider.它主要用来生成实例rootScope,它代表angularjs应用的根 ...
- 1.HTML练习(二)
一.表格练习: 1.<table>标签:声明一个表格,它的常用属性如下: border属性 定义表格的边框,设置值是数值 cellpadding属性 定义单 ...
- springMVC请求注解@RequestMapping各个属性值
最近遇到了一个采用fastJson传输数据的方式,搞了半天,总是感觉模糊,觉得自己有必要在这里做一个系统的总结,今天先从@RequestMapping的属性开始,采用REST 风格的 URL 请求,R ...
- 数据分析:pandas 基础
pandas 是基于 Numpy 构建的含有更高级数据结构和工具的数据分析包 类似于 Numpy 的核心是 ndarray,pandas 也是围绕着 Series 和 DataFrame 两个核心数据 ...
- 队列的理解和实现(二) ----- 链队列(java实现)
什么是链队列 链队是指采用链式存储结构实现的队列,通常链队用单链表俩表示.一个链队显然需要两个分别指示队头和队尾的指针,也称为头指针和尾指针,有了这两个指针才能唯一的确定. package 链队列; ...
- 实验一 c++简单程序设计
一.实验内容 1.ex 2_28 (1) 用if...else判断 #include<iostream> using namespace std; int main() { char i; ...
- PS2模拟器 PCSX2 新手向
1.模拟器的下载 1.1百度网盘地址:http://pan.baidu.com/s/1i3kt7bJ (已经整合了PS2BIOS的模拟器下载,比较新的版本,适合新手) 1.2高端玩家可以下载: 官网g ...
- 内核诊断(二)-- patch 和diff
patch文件结构 生成patch文件 --diff命令 patch 使用 -- patch命令 3.1 打path 3.1撤销patch 使用举例 4.1 基本命令使用 4.2 内核打补丁 1. p ...
- (转)Asp.Net Mvc视图引擎Razor介绍
Asp.Net Mvc视图引擎Razor介绍 1.Razor介绍 程序园原创,转载请注明:http://www.kwstu.com/ArticleView/dabaomvc_2014082408205 ...