题目描述

找出给出的字符串S中最长的回文子串。假设S的最大长度为1000,并且只存在唯一解。

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.
示例1

输入

复制

"abcba"

输出

复制

"abcba"

动态规划

class Solution {
public:
    /**
     *
     * @param s string字符串
     * @return string字符串
     */
    string longestPalindrome(string str) {
        // write code here
        int n=str.length();
        if (n==0) return "";
        bool dp[n][n];
        fill_n(&dp[0][0],n*n,false);
        int left=0,right=0,maxLen=0;
        for (int j=0;j<n;j++)
        {
            dp[j][j]=true;
            for (int i=0;i<j;i++)
            {
                dp[i][j]=(str[i]==str[j] && (j-i<2 || dp[i+1][j-1]));
                if (dp[i][j]&& (j-i+1>maxLen))
                    {
                        left=i;
                        right=j;
                        maxLen=j-i+1;
                    }
            }
            
        }
                    return str.substr(left,right-left+1);
    }
};

leetcode144 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. 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 le ...

  10. Leetcode5:Longest Palindromic Substring@Python

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

随机推荐

  1. Java 读取文件中的每一行,并为每一行插入特定的字符串

    工具 1:Eclipse Java EE IDE for Web Developers. Version: Photon Release (4.8.0). Build id: 20180619-120 ...

  2. apt-get 安装软件时出现:“文件尺寸不符” 问题

    报错信息 命中:1 http://packages.deepin.com/deepin panda InRelease 命中:2 http://linux.teamviewer.com/deb sta ...

  3. regsvr32 bypass windows defender 新思路

    原文链接:blog 在对regsvr32的用法进行了解之后,对于Casey Smith的远程js脚本执行命令的思路很感兴趣. 命令语法如下: regsvr32 /s /n /u /i:http://1 ...

  4. springCloud微服务调用失败【CannotGetJdbcConnectionException: Failed to obtain JDBC Connection】

    详情如下: 2019-07-28 10:56:18.229 ERROR 16212 --- [nio-8081-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet ...

  5. 网页添加 Live2D 看板娘

        我是先参考别人的[点击跳转]博客来做的.不过我发现网上很多人都没有把一些细节写出来,用了别人那里下载的文件后里面的一些跳转链接就跳到他们的页面了.所以我这里写一写如何修改这些跳转链接吧. 1. ...

  6. swoole为什么不建议使用static和global

    $http = new swoole_http_server("0.0.0.0", 9501); $http->on("request", functio ...

  7. 往with as中写入数据的方法

    方法1:直接写入,使用union all,简单直观,但程序运行效率低,几百条就很慢了 with dw_wms_outbound_info_v100 as( select '10700001' as o ...

  8. 源代码 VS 汇编代码 VS 目标代码 VS 字节码 VS 机器码

    1.源代码(source code) 源代码就是平时我们开发的代码:比如C.Java.Python.Shell...等 public class HelloWorld { public static ...

  9. ERP仓库管理的操作与设计--开源软件诞生20

    赤龙ERP库房管理讲解--第20篇 用日志记录"开源软件"的诞生 [点亮星标]----祈盼着一个鼓励 博主开源地址: 码云:https://gitee.com/redragon/r ...

  10. maven项目导入并运行

    idea导入maven工程流程 找到要导入的文件位置 打开导入 选择manve 一直next就好 选择jdk,选择自己的jdk--home就可以 点击finished 等待坐标导入,查看右侧maven ...