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 ...
随机推荐
- Ajax学习重点总结
1.什么是AJAX AJAX=Asynchronous JavaScript and XML(异步的JavaScript和XML). AJAX是在不重新加载整个页面的情况下,后台与服务器交换数据并更新 ...
- centos7下安装nodejs
http://www.runoob.com/nodejs/nodejs-install-setup.html这个教程中注明 注意:Linux上安装Node.js需要安装Python 2.6 或 2.7 ...
- linux-命令-ls
一.命令介绍: ls命令是linux常用的命令之一.ls用来打印当前目录的文件清单或指定目录的文件清单,也可以查看到文件的基本权限和隐藏文件. 二.命令格式: ls [OPTION]... [FILE ...
- ThinkPHP 自动验证相关注意
1.假如加入了表单令牌的话,表单的各种名与对应Model字段一致,不然报坑爹的令牌错误. 2.假如加入了表单令牌的话,Create只能采用默认的POST数据创建数据对象,不然又是坑爹的令牌错误. 3. ...
- WPF中未将对象引用设置到对象的实例
前几天,我开始了WPF的基础学习,一上来我就遇到了一个令我头痛的问题,按照书上的例子我写了一段属于自己的代码,一个简单的色调器.满心期待的编译运行,就出现了未将对象引用设置到对象的实例.我在网上查阅了 ...
- mongodb C# 驱动查询
INoSqlProvider provider = NoSqlManager.Create("CloudTable"); IMongoCollection<FormMongo ...
- 判断IP地址的类型
#include <stdio.h> #include <stdlib.h> void main() { ]; int ip_addr; printf("请输入IP地 ...
- linux 常见问题&解决方案
Q1: 错误重现: linux下 用tar -zxf xxx.tar.bz2 报错: gzip: stdin: not in gzip format tar: Child returned statu ...
- kernel/info.c
/* * linux/kernel/info.c * * Copyright (C) 1992 Darren Senn */ /* This implements the sysinfo() syst ...
- 转:仿QQ长按弹出功能菜单
android app普遍常用的弹出上下文菜单是列表式的.QQ列表,在上方弹出冒泡效果菜单,如图 这里讲解一下如何写出这个效果: 图片资源:两张图片,可以拼接成如上效果. 首先,我们需要写布局文件: ...