5. Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of Sis 1000, and there exists one unique longest palindromic substring.
==
class Solution {
public:
string longestPalindrome(string s) {
/**bool dp[i][j]代表s[i-j]是否回文
dp[i][j] = true ;i==j
= s[i]==s[j] ;j=i+1
= (s[i]==s[j])&& dp[i+1][j-1]; j>i+1
要记住,dp[i][j]表示的i-j之间的字符串是不是回文,
当i==j时,dp[i][i]当然是回文
当j=i+1时,dp[i][j],要看字符串s[i]和s[j]之间是不是相同的?
当j>i+1时,dp[i][j]的判定情况,s[i]s[j]和dp[i+1][j-1]共同决定的;
怎么开始的?
外层循环j控制,
内存循环i判断当前能到达的位置是否是回文;
*/
int n = s.size();
bool dp[n][n];
fill_n(&dp[][],n*n,false);///初始化
int max_len = ;
int start = ;
for(int j = ;j<s.size();j++){
for(int i = ;i<=j;i++){
if(i==j) dp[i][j] = true;
else if(j==(i+)) dp[i][j] = s[i]==s[j]?true:false;
else if(j>(i+)) dp[i][j] = s[i]==s[j]&&dp[i+][j-];
if(dp[i][j]&&max_len < (j-i+)){
max_len = j-i+;
start = i;
}
}///for
}
return s.substr(start,max_len);
}
};
5. Longest Palindromic Substring的更多相关文章
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- leetcode--5. Longest Palindromic Substring
题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- No.005:Longest Palindromic Substring
问题: 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 ...
- 【leedcode】 Longest Palindromic Substring
Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...
- [LeetCode_5] Longest Palindromic Substring
LeetCode: 5. Longest Palindromic Substring class Solution { public: //动态规划算法 string longestPalindrom ...
- leetcode-【中等题】5. Longest Palindromic Substring
题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...
- Leetcode5:Longest Palindromic Substring@Python
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
随机推荐
- The file couldn't be opened because you don't have permission to view it
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- 第一章Android系统移植与驱动开发概述--读书笔记
以前,初步学习过嵌入式Linux驱动开发的基础课程,对于驱动开发可以说是有了一点点微末的基础吧.首先我们要对Android嵌入式系统有一个初步的认识,Android系统发展到今天已经具备了完善的架构. ...
- Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- extJS起步
万事开头难,不过还好有解决办法——参考如下文章 http://blog.csdn.net/leimengyuanlian/article/details/18748599 可以快速搭建好eclipse ...
- C++预处理详解
本文在参考ISO/IEC 14882:2003和cppreference.com的C++ Preprocessor的基础上,对C++预处理做一个全面的总结讲解.如果没有特殊说明,所列内容均依据C++9 ...
- Unity C# XmlDoc.LoadXml() il2cpp not implemented exception
Stuck with this for a couple of hours, turned out it's just a setting thing, Answer from this post ( ...
- 八皇后—Java
package queen; public class queen { static boolean col[] = new boolean[8]; static boolean main_diago ...
- HTML meta 标签用法(转)
meta主要为分HTTP标头信息(HTTP-EQUIV)和页面描述信息(NAME).标头信息包括文档类型.字符集.语言等浏览器正确显示网页的信息及处理动作:网页描述如内容的关键字.摘要.作者和定义ro ...
- windows环境下创建 .文件夹
一.windows环境下创建 .文件夹 1.新建一个文件夹 2.重命名为.properties.(名字前后都加点) 二.windows环境下创建 .文件 1.上面的方法对文件同样适用 2.运行CMD, ...
- poj2368 Buttons Nim取石子游戏
链接:http://poj.org/problem?id=2368 和前面差距还是很大啊囧 代码: k,a;main(i){,i=;i<=k/&&k%i;++i);k%i||(a ...