[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)级别的时间 ...
随机推荐
- [JS] 屏蔽右键
if (window.Event) document.captureEvents(Event.MOUSEUP); function nocontextmenu() { event.cancelBubb ...
- C#-WebForm-★★★JQuery知识——基础知识、选择器、事件★★★
JQuery 与 JS 之间的转换 将JQuery转换为JS —— get(0) 例如:alert( $("#d1").get(0).offsetwidth ); 将JS 转换为J ...
- Android NDK开发及OpenCV初步学习笔记
https://www.jianshu.com/p/c29bb20908da Android NDK开发及OpenCV初步学习笔记 Super_圣代 关注 2017.08.19 00:55* 字数 6 ...
- LTE:EPC
User Identifiers - IMSI and GUTI IMSI A globle id that unique identifies a subscribe.It composed t ...
- windows下python3.7.2内置venv虚拟环境下pyinstaller错误问题
起因 开发一直使用python -m venv .pyenv 方式创建虚拟环境,在利用pyinstaller打包发布应用时,出现错误 3178 INFO: Warnings written to C: ...
- (转)OpenStack之服务端口号
原文:https://blog.csdn.net/henulwj/article/details/47276391 在部署openstack的过程中,你会遇到配置各种服务的endpoint,opens ...
- TortoiseGit学习系列之Windows上TortoiseGit的安装详解(图文)
不多说,直接上干货! TortoiseGit的安装准备 首先你得安装windows下的msysgit. 安装版本控制器客户端TortoiseGit [不习惯英文的朋友,也可以下个语言包]. 下载地址: ...
- Java 正则表达式 中的 任意字符
原来正则表达式中的"."代表的是除换行以外的任意字符,如果要真正代表任意字符,需要把换行符也加进去,但是经过测试"[.\\n]"不生效,可以使用"\\ ...
- C#调用SQLite报错:无法加载 DLL“SQLite.Interop.dll”: 找不到指定的模块
C#调用SQLite数据库,有些情况下会报以下这个错误: 无法加载 DLL“SQLite.Interop.dll”: 找不到指定的模块 实际上程序目录中是存在SQLite.Interop.dll这个文 ...
- Python基础 - 总则
学习Python的笔记,有基础语法,有注意点.仅此而已. 目录: ------------------------------------------- Python基础(1) - 初识Python ...