LN : leetcode 5 Longest Palindromic Substring
lc 5 Longest Palindromic Substring
5 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"
DP Accepted
看题目的标题就应该可以猜到这一题可以借助动态规划求解最优解。用数组str[i][j]表示从第i-1到第j-1个的子串是一个回文。首先,对于所有单个字符来说,本身就是一个回文,长度为1;而对于两个连在一起的字符来说,如果相同,那么回文长度就为2。以上是问题最简单的例子,考虑复杂的情况,依次寻找长度为3以上的回文,如果首尾相同且首尾之间是回文,则更新记录的回文长度和起始位置。
class Solution {
public:
string longestPalindrome(string s) {
int begin = 0, len = s.size(), max = 1;
bool str[1000][1000] = {false};
for (int i = 0; i < len; i++) str[i][i] = true;
for (int i = 0; i < len-1; i++) {
if (s[i] == s[i+1]) {
max = 2;
begin = i;
str[i][i+1] = true;
}
}
for (int l = 3; l <= len; l++) {
for (int i = 0; i < len-l+1; i++) {
int j = l+i-1;
if (s[i] == s[j] && str[i+1][j-1]) {
max = l;
begin = i;
str[i][j] = true;
}
}
}
return s.substr(begin, max);
}
};
LN : leetcode 5 Longest Palindromic Substring的更多相关文章
- LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法
LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...
- 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
写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道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(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- [LeetCode][Python]Longest Palindromic Substring
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 【LeetCode】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
- [LeetCode] 5. Longest Palindromic Substring 最长回文子串
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
随机推荐
- 洛谷—— P3370 【模板】字符串哈希
P3370 [模板]字符串哈希 题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的想好好 ...
- 使用URL Rewrite实现网站伪静态
下载urlwrite包 将urlrewrite-***.jar复制到web应用lib文件夹下 web.xml中配置URL Rewrite: 例: <filter> <filter-n ...
- tmux还有这种操作,我在这边的窗口上操作,你那边可以实时的看到我的操作,厉害了
- [React] Build a slide deck with mdx-deck using Markdown + React
In this lesson we'll use mdx-deck to create a slide deck using Markdown and React. We'll look at add ...
- linux用户进程分析
经过实验3的介绍.我们须要来点实在的.所以将我们理解的流程用于linux系统的分析.换句话说.通过类比的方式去进行描写叙述与理解linux相关的部分. 本节的内容非常详实.并且也分析 ...
- 开拓新途径找出新方法,上海SEO公司分享3个操作看看是否可行
开拓新途径找出新方法,上海SEO公司分享3个操作看看是否可行 内容收录,外链公布,流量点击.用户体验.这是SEO优化的几个核心和重点.也是SEO站长每天都在绞尽脑汁进行操作的SEO重心,影响着非常多人 ...
- [计算机故障处理]EXCEL文件双击不能直接打开
同事的电脑里的EXCEL文件不知什么原因双击不能直接打开了,双击只能打开软件而且是没有任何表格的,但通过软件中的“打开”再找到指定的文件能打开. 解决方案: 打开excel,依次选择:工具-选项-常规 ...
- Java后端发出post请求带参数并接收返回的json
核心代码: 参数格式: “key1=value1&key2=value2” /*** sendUrl (远程请求的URL)* param (远程请求参数)* JSONObject ...
- document.getElementsByClassName("head")[0].parentElement
document.getElementsByClassName("head")[0].parentElement
- Jenkins重启 在Windows GUI上
To restart Jenkins manually, you can use either of the following commands: (jenkins_url)/safeRestart ...