zoukankan      html  css  js  c++  java
  • leetcode144 longest-palindromic-substring

    题目描述

    找出给出的字符串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);
        }
    };
  • 相关阅读:
    WebService是什么?以及工作原理
    分布锁的问题?
    反射是什么?原理?作用?
    HTTP/1.1与HTTP/1.0的区别
    Ajax的跨域问题(包括解决方案)?
    SVN与Git优缺点比较
    类的加载过程?
    B树, B-树,B+树,和B*树的区别
    Linux常用的50个命令
    权限模型
  • 原文地址:https://www.cnblogs.com/hrnn/p/13352990.html
Copyright © 2011-2022 走看看