• 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.

    class Solution {
    public:
        string longestPalindrome(string s) 
        {
            int n=s.length();
            int longest[n];
            int max=1;
            int pend=0;
            //1 3 5 
            for(int i=0;i<n;i++) longest[i]=1;
            
            for(int l=3;l<=n;l=l+2)
            {
                for(int i=n-1;i>=-1;i--)
                    if(longest[i-1]==l-2 && s[i]==s[i-l+1])
                    {
                        longest[i]=l;
                        if(max<l)
                        {
                            max=l;pend=i;
                        }
                    }
            }
            //2 4 6
            for(int i=0;i<n;i++) longest[i]=0;
            for(int l=2;l<=n;l=l+2)
            {
                for(int i=n-1;i>=-1;i--)
                    if(longest[i-1]==l-2 && s[i]==s[i-l+1])
                    {
                        longest[i]=l;
                        if(max<l)
                        {
                            max=l;pend=i;
                        }
                    }
            }
            string result="";
            for(int i=pend;i>=pend-max+1;i--)
                result=s[i]+result;
            return result;
        }
    }; 
  • 相关阅读:
    ES vm报错
    ln -s /usr/local/jdk1.8.0_201/bin/java /bin/java
    docker压缩导入导出
    微软各种资源整理(迅雷下载),感谢站长。
    python打开文件的访问方式
    docker换源
    selinux
    ElasticsearchException: java.io.IOException: failed to read [id:0, file:/data/elasticsearch/nodes/0/_state/global-0.st]
    带了纸和笔,要记哪些东西?
    redis命令行批量删除匹配到的key
  • 原文地址:https://www.cnblogs.com/erictanghu/p/3759177.html
Copyright © 2020-2023  润新知