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的更多相关文章

  1. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  2. leetcode--5. Longest Palindromic Substring

    题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

  3. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  4. No.005:Longest Palindromic Substring

    问题: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  5. Leetcode Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  6. 【leedcode】 Longest Palindromic Substring

    Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...

  7. [LeetCode_5] Longest Palindromic Substring

    LeetCode: 5. Longest Palindromic Substring class Solution { public: //动态规划算法 string longestPalindrom ...

  8. leetcode-【中等题】5. Longest Palindromic Substring

    题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...

  9. Leetcode5:Longest Palindromic Substring@Python

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

随机推荐

  1. [转]jq选择器

    jQuery-强大的jQuery选择器 (详解)[转] 1. 基础选择器 Basics 名称 说明 举例 #id 根据元素Id选择 $("divId") 选择ID为divId的元素 ...

  2. IOS atomic与nonatomic,assign,copy与retain的定义和区别

    IOS atomic与nonatomic,assign,copy与retain的定义和区别 atomic和nonatomic用来决定编译器生成的getter和setter是否为原子操作.        ...

  3. 关于npm

    转载自AlloyTeam:http://www.alloyteam.com/2016/03/master-npm/ 这是我学npm觉得最好的一篇文章啦-大家一起学起来吧 npm本来是Node.js的包 ...

  4. Asp.net Web.Config - 配置元素 caching

    Asp.net Web.Config - 配置元素 caching 记得之前在写缓存DEMO的时候,好像配置过这个元素,好像这个元素还有点常用. 一.caching元素列表   元素 说明 cache ...

  5. codeforces 723E (欧拉回路)

    Problem One-Way Reform 题目大意 给一张n个点,m条边的无向图,要求给每条边定一个方向,使得最多的点入度等于出度,要求输出方案. 解题分析 最多点的数量就是入度为偶数的点. 将入 ...

  6. 《看板与Scrum》读书笔记

    看板的朴素思想:在制品(work-in-progress, WIP)必须被限制 WIP上限和拉动式生产 1. Scrum与看板简述 Scrum:组织拆分,工作拆分,开发时间拆分,优化发布计划,过程优化 ...

  7. HttpContext详解

    HttpContext 类:封装有关个别 HTTP 请求的所有 HTTP 特定的信息. 在处理请求执行链的各个阶段中,会有一个对象在各个对象之间进行传递,也即会保存 请求的上下文信息,这个对象就是Ht ...

  8. mvc3 上传图片

    这是在Control里使用的代码,是在后台管理需要上传图片时使用的,不过我在这犯了一个错误, Request.Files[inputName];inputName名字中的大小写<input ty ...

  9. Java高级规范之三

    三十一.如果变量名要加注释,说明命名不是很准确. 不规范示例:暂无 规范实例:暂无 解析:暂无 三十二.任何类字段除非必要,否则都要私有化 不规范示例: public class Person{ St ...

  10. Azure 媒体服务可将优质内容传输至 Apple TV

    作为内容提供商,如果想要将优质内容传输到Apple TV,需要使用Apple FairPlay Streaming (FPS)技术. 但是这个技术的构建比较繁琐,基于此,Azure提供了FairPla ...