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. windows下开启mysql表名大小限制

    找到my.ini文件 在文件中找到 [mysqld]的下面, 看有没有 [mysqld] lower_case_table_names 的设置, 如果没有 添加lower_case_table_nam ...

  2. Device eth0 does not seem to be present, delaying initialization(解决克隆CentOS6.3虚拟机后网卡设备无法启动问题)

    1.删除 /etc/udev/rules.d/70-persistent-net.rules 后重启机器 2.重新启动之后,把/etc/udev/rules.d/70-persistent-net.r ...

  3. python-selenium之firefox、Chrome、Ie运行

    测试脚本是否支持在不同浏览器运行firefox浏览器运行脚本 from selenium import webdriver driver=webdriver.Firefox() driver.get( ...

  4. 项目用到的icarouls类和UIEffectDesignerView类,菜单技巧,构思(金方圆)

    // //  MenuHomeViewController.m //  HFYS // //  Created by Showsoft_002 on 13-8-14. //  Copyright (c ...

  5. python webdriver 自动化测试练习 1-- 在线调查

    __author__ = 'Mickey0s' # coding:utf8 from selenium import webdriver from selenium.webdriver.common. ...

  6. 去掉win10桌面小图标

    参考:http://bbs.kafan.cn/thread-1843802-1-1.html

  7. day27_反射

    1.反射-概述(掌握) 反射就是在程序运行过程中,通过.class文件动态的获取类的信息(属性,构造,方法),并调用 注意:JAVA不是动态语言,因为动态语言强调在程序运行过程中不仅能获取并调用类里面 ...

  8. pylab,matplotlib Invalid DISPLAY variable

    在cetos 服务器使用源码包,安装matplotlib, 安装成功后, import pylab as pl pl.figure(figsize=(16,8)) python 解析器报错,Inval ...

  9. java的覆盖重写隐藏和C#中的不同

    先看下C#中的: C#中覆盖 隐藏 重写这三种有不同的意义,而Java中不同. 1. java中没有new ,使用new会报错,编译不通过. 2. java中重写和覆盖应该是一个意思 static c ...

  10. ASP.NET Core (Database First)

    CREATE DATABASE [EFCore_dbfirst] GO USE [EFCore_dbfirst] GO CREATE TABLE [Blog] ( [BlogId] int NOT N ...