【LeetCode】5. Longest Palindromic Substring 最长回文子串
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
公众号:负雪明烛
本文关键词:最长回文子串,题解,leetcode, 力扣,python, C++, java
题目地址:https://leetcode.com/problems/longest-palindromic-substring/description/
题目描述
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"
题目大意
找出字符串中最长的回文子串。
解题方法
暴力遍历
遍历算法是我们最直观的解法,事实上也能通过OJ。我们使用的方法是两重循环确定子串的起始和结束位置,这样只要判断该子串是个回文,我们保留最长的回文即可。
代码很简单,C++版本如下:
class Solution {
public:
string longestPalindrome(string s) {
const int N = s.size();
string res;
for (int i = 0; i < N; i++) {
for (int j = i; j < N; j++) {
if (j - i + 1 >= res.size() && isPalindrome(s, i, j)) {
res = s.substr(i, j - i + 1);
}
}
}
return res;
}
// [start, end]
bool isPalindrome(string& s, int start, int end) {
const int N = s.size();
int l = start, r = end;
while (l <= r) {
if (s[l++] != s[r--]) {
return false;
}
}
return true;
}
};
动态规划
动态规划的两个特点:第一大问题拆解为小问题,第二重复利用之前的计算结果,来解答这道题。
那如何划分小问题呢,我们可以先把所有长度最短为1的子字符串计算出来,根据起始位置从左向右,这些必定是回文。然后计算所有长度为2的子字符串,再根据起始位置从左向右。到长度为3的时候,我们就可以利用上次的计算结果:如果中心对称的短字符串不是回文,那长字符串也不是,如果短字符串是回文,那就要看长字符串两头是否一样。这样,一直到长度最大的子字符串,我们就把整个字符串集穷举完了。
我们维护一个二维数组 dp,其中 dp[i][j] 表示字符串区间 [i, j] 是否为回文串。
- 当
i = j时,只有一个字符,肯定是回文串; - 如果
i = j + 1,说明是相邻字符,此时需要判断s[i]是否等于s[j]; - 如果
i和j不相邻,即i - j >= 2时,除了判断s[i]和s[j]相等之外,dp[j + 1][i - 1]若为真,就是回文串。
通过以上分析,可以写出递推式如下:
dp[i, j] = 1 if i == j
= s[i] == s[j] if j = i + 1
= s[i] == s[j] && dp[i + 1][j - 1] if j > i + 1
Python 代码刚提交的时候超时了,但是使用set一下,看看是否只包含相同字符,这样就通过了!
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
if len(set(s)) == 1: return s
n = len(s)
start, end, maxL = 0, 0, 0
dp = [[0] * n for _ in range(n)]
for i in range(n):
for j in range(i):
dp[j][i] = (s[j] == s[i]) & ((i - j < 2) | dp[j + 1][i - 1])
if dp[j][i] and maxL < i - j + 1:
maxL = i - j + 1
start = j
end = i
dp[i][i] = 1
return s[start : end + 1]
C++版本代码如下,需要注意的是这里的res初始化为第一个字符:
class Solution {
public:
string longestPalindrome(string s) {
const int N = s.size();
if (N == 0) return "";
string res = s.substr(0, 1);
vector<vector<bool>> dp(N, vector(N, false));
// s[j, i]
for (int i = 0; i < N; i++) {
for (int j = 0; j < i; j++) {
dp[j][i] = (s[j] == s[i]) && (i == j + 1 || dp[j + 1][i - 1]);
if (dp[j][i] && i - j + 1 >= res.size()) {
res = s.substr(j, i - j + 1);
}
}
dp[i][i] = true;
}
return res;
}
};
二刷—
马拉车算法。。待续
参考:
http://www.cnblogs.com/grandyang/p/4464476.html
https://segmentfault.com/a/1190000002991199
日期
2018 年 3 月 15 日 —— 雾霾消散,春光明媚
2019 年 1 月 19 日 —— 有好几天没有更新文章了
【LeetCode】5. Longest Palindromic Substring 最长回文子串的更多相关文章
- Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)
Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...
- [LeetCode] 5. 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最长回文子串
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 ...
- lintcode :Longest Palindromic Substring 最长回文子串
题目 最长回文子串 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串. 样例 给出字符串 "abcdzdcab",它的最长回文 ...
- 5. Longest Palindromic Substring(最长回文子串 manacher 算法/ DP动态规划)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- 1. Longest Palindromic Substring ( 最长回文子串 )
要求: Given a string S, find the longest palindromic substring in S. (从字符串 S 中最长回文子字符串.) 何为回文字符串? A pa ...
- 【翻译】Longest Palindromic Substring 最长回文子串
原文地址: http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-i.html 转载请注明出处:http:// ...
- 005 Longest Palindromic Substring 最长回文子串
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
随机推荐
- 1.TwoSum-Leetcode
#include<iostream> #include<algorithm> #include<map> using namespace std; class So ...
- 关于写SpringBoot+Mybatisplus+Shiro项目的经验分享三:问题2
框架: SpringBoot+Mybatisplus+Shiro 简单介绍:关于写SpringBoot+Mybatisplus+Shiro项目的经验分享一:简单介绍 搜索框是该项目重要的一环,由于涉及 ...
- CSS3单行文本两端对齐
CSS3实现单行文本两端对齐 p { height: 24px; text-align: justify; text-last-align: justify; } p::after { display ...
- doy05循环语法学习笔记
doy05循环语法学习笔记 一.while循环语法: 1.基本用法示例 x = 1 while x <= 5: print(x) x += 1 2.死循环:永远不结束的循环 如:while Tr ...
- AOP中环绕通知的书写和配置
package com.hope.utils;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotatio ...
- Anaconda+pycharm(jupyter lab)搭建环境
之前先是安装了pycharm,手动安装了python2.7和3.7版本,在pycharm里面使用alt+/手动下载包.后来想使用jupyter lab,手动下载包太麻烦且有版本管理的文艺,于是打算装A ...
- Docker从入门到精通(一)——初识
1.Docker 是什么? Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容 ...
- 重新整理 .net core 实践篇——— UseEndpoints中间件[四十八]
前言 前文已经提及到了endponint 是怎么匹配到的,也就是说在UseRouting 之后的中间件都能获取到endpoint了,如果能够匹配到的话,那么UseEndpoints又做了什么呢?它是如 ...
- 并行Louvain社区检测算法
因为在我最近的科研中需要用到分布式的社区检测(也称为图聚类(graph clustering))算法,专门去查找了相关文献对其进行了学习.下面我们就以这篇论文IPDPS2018的文章[1]为例介绍并行 ...
- [BUUCTF]REVERSE——[GUET-CTF2019]re
[GUET-CTF2019]re 附件 步骤: 查壳儿,upx壳,64位程序 upx脱壳儿,然后扔进64位ida,通过检索字符串,找到有关flag的信息定位到关键函数 让我们输入flag,然后满足su ...