题目描述

找出给出的字符串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的string方法使用

    1.将list转换为","隔开的字符串 //videoIdList值转换成 1,2,3 String videoIds = StringUtils.join(videoIdList ...

  2. 1.入门篇十分钟了解Spring Cloud

    文章目录 Spring Cloud入门系列汇总 为什么需要学习Spring Cloud 什么是Spring Cloud 设计目标与优缺点 设计目标 优缺点 Spring Cloud发展前景 整体架构 ...

  3. ORA-00017: session requested to set trace event 请求会话以设置跟踪事件

    ORA-00017: session requested to set trace event   ORA-00017: 请求会话以设置跟踪事件 Cause:       The current se ...

  4. unix中的线程池技术详解

    •线程池就是有一堆已经创建好了的线程,当有新的任务需要处理的时候,就从这个池子里面取一个空闲等待的线程来处理该任务,当处理完成了就再次把该线程放回池中,以供后面的任务使用,当池子里的线程全都处理忙碌状 ...

  5. 汕尾6397.7539(薇)xiaojie:汕尾哪里有xiaomei

    汕尾哪里有小姐服务大保健[微信:6397.7539倩儿小妹[汕尾叫小姐服务√o服务微信:6397.7539倩儿小妹[汕尾叫小姐服务][十微信:6397.7539倩儿小妹][汕尾叫小姐包夜服务][十微信 ...

  6. C++分隔字符串split

    split C++标准库中没有提供split分隔字符串的函数,哎. 实现一 下面的实现需要指定分隔符的集合delimiters,以及是否将连续的分隔符看作同一个分隔compress : enum cl ...

  7. 数据查询语句:DQL(Data Query Language)

    一.基础查询 1.语法:select 查询列表 from 表名; 2.特点:1.通过select查询完的结果,是一个虚拟的表格,不是真实存在   2.查询列表可以是:字段.表达式.常量.函数等   3 ...

  8. go操作mysql数据库

    Golang GORM使用 gorm gorm是go语言中实现数据库访问的ORM(对象关系映射)库.使用这个库,我们可以利用面向对象的方法,更加方便的对数据库中的数据进行CRUD(增删改查). 基本使 ...

  9. git学习(七) git的标签

    git的标签操作 git标签操作 git tag 不加任何参数 表示显示标签(按字母序) 非按时间 git tag 标签名 默认是给最近一次提交打上标签 git tag 标签名 commitId 给响 ...

  10. Qt导入CMakeLists.txt后无法调试

    问题: Qt导入CMakeLists.txt后无法单步调试 解决方法: 在CMakeLists.txt后加入一句: SET(CMAKE_BUILD_TYPE DEBUG)