题目来源:https://leetcode.com/problems/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, and there exists one unique longest palindromic substring.

动态规划,类似于lcs的解法,数组flag[i][j]记录s从i到j是不是回文

首先初始化,i>=j时,flag[i][j]=true,这是因为s[i][i]是单字符的回文,当i>j时,为true,是因为有可能出现flag[2][1]这种情况,比如bcaa,当计算s从2到3的时候,s[2]==s[3],这时就要计算s[2+1] ?= s[3-1],总的来说,当i>j时置为true,就是为了考虑j=i+1这种情况。

接着比较s[i] ?= s[j],如果成立,那么flag[i][j] = flag[i+1][j-1],否则直接flag[i][j]=false

c++代码:

  1. class Solution {
  2. public:
  3. string longestPalindrome(string s) {
  4. int len = s.length(), max = , ss = , tt = ;
  5. bool flag[len][len];
  6. for (int i = ; i < len; i++)
  7. for (int j = ; j < len; j++)
  8. if (i >= j)
  9. flag[i][j] = true;
  10. else flag[i][j] = false;
  11. for (int j = ; j < len; j++)
  12. for (int i = ; i < j; i++)
  13. {
  14. if (s[i] == s[j])
  15. {
  16. flag[i][j] = flag[i+][j-];
  17. if (flag[i][j] == true && j - i + > max)
  18. {
  19. max = j - i + ;
  20. ss = i;
  21. tt = j;
  22. }
  23. }
  24. else flag[i][j] = false;
  25. }
  26. return s.substr(ss, max);
  27. }
  28. };

LeetCode 5 Longest Palindromic Substring(最长子序列)的更多相关文章

  1. LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法

    LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...

  2. Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

    Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...

  3. 求最长回文子串 - leetcode 5. Longest Palindromic Substring

    写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...

  4. 【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 ...

  5. leetcode:Longest Palindromic Substring(求最大的回文字符串)

    Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...

  6. [LeetCode][Python]Longest Palindromic Substring

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  7. 【LeetCode】Longest Palindromic Substring 解题报告

    DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...

  8. [LeetCode] 5. Longest Palindromic Substring 最长回文子串

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

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

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

随机推荐

  1. debian之samba服务器搭建

    安装过程非常简单: apt-get install samba sudo vim /etc/sama/smb.conf [pengdl] comment = pengdl's samba path = ...

  2. 微软IIS对http keep-alive的“霸道”处理

    大家都知道在IIS中有个HTTP keep-alive设置,见下图: 很多人可能和我们一样,以为这样设置后,IIS会就在发送响应内容时加上这个http header——Connection: keep ...

  3. Scrum 3.2 多鱼点餐系统开发进度(页面优化&下单详细信息页面)

    Scrum 3.2 多鱼点餐系统开发进度(页面优化&下单详细信息页面)  1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选 ...

  4. Res_Orders_01之需求分析

    Res_Orders_01之需求分析 一.背景及好处 为了提高餐厅的运营效率,增强餐厅各部门间的配合,减少顾客到店后的点餐.等餐及结算过程消耗的时间,降低服务员点餐失误率,进一步提高餐厅管理人员对菜品 ...

  5. nopcommerce3.5源代码及中文语言包下载地址

    nopcommerce3.5源代码下载地址 http://download-codeplex.sec.s-msft.com/Download/SourceControlFileDownload.ash ...

  6. How to Convert Subversion Repo to Git

    用了比较长时间的 SVN,但现在新的项目都采用Git.之前的项目又不得不维护,那么能不能将项目从SVN迁移到Git呢.答案是肯定的,网上的方案是 git-svn,或者更高级的封装 svn2git. 方 ...

  7. 重构第29天 去除中间人对象(Remove Middle Man)

    理解:本文中的”去除中间人对象”是指把 在中间关联而不起任何其他作用的类移除,让有关系的两个类直接进行交互. 详解:有些时候在我们的代码会存在一些”幽灵类“,设计模式大师Martin Fowler称它 ...

  8. 创建一个带模版的用户控件 V.2

    前面有做练习<创建一个带模版的用户控件>http://www.cnblogs.com/insus/p/4161544.html .过于简化.通常使用数据控件Repeater会有网页写好He ...

  9. ref与out的区别

    若要使用 ref 参数,方法定义和调用方法均必须显式使用 ref 关键字,如下面的示例所示. class RefExample { static void Method(ref int i) { // ...

  10. sencha grid templatecolumn模板列,actioncolumn和renderer实现单元格重绘

    templatecolumn列: {                                     xtype: 'templatecolumn',                     ...