• LeetCode the longest palindrome substring


    回文检测,参考http://blog.csdn.net/feliciafay/article/details/16984031

    使用时间复杂度和空间复杂度相对较低的动态规划法来检测,具体的做法

    图一 偶数个回文字符情况

    图二 奇数个回文字符情况

    核心就是如果一个子串是回文,如果分别向回文左右侧扩展一个字符相同,那么回文就向外扩展一位。

    实现的代码如下

    bool table[1000][1000] = {false};
        int sStart = 0;
        int iLength = 1;
        int n = s.size();
    
        for(int iLoop = 0; iLoop < n; ++iLoop)
        {
            table[iLoop][iLoop] = true;
        }
        for(int iLoop = 0; iLoop < n-1; ++iLoop)
        {
            if(s[iLoop] == s[iLoop+1])
            {
                table[iLoop][iLoop+1] = true;
                sStart = iLoop;
                iLength = 2;
            }
        }
        for(int  len= 3; len <= n; ++len)
        {
            for(int  i = 0; i < n - len + 1; ++i)
            {
               int  j = len + i -1;
                if(s[i] == s[j] && table[i+1][j-1] )
                {
                    table[i][j] = true;
                    sStart = i;
                    iLength = len;
                }
            }
        }
        return s.substr(sStart, iLength);

     

     

  • 相关阅读:
    vue
    生成数组方式
    绕过CDN查找真实IP方法
    SQL注入WAF绕过
    缓冲区溢出的保护机制
    Redis未授权漏洞
    AFL 漏洞挖掘
    python多线程与多进程
    Java8四大核心接口示例
    @Autowired抱空指针异常解决方案
  • 原文地址:https://www.cnblogs.com/bestwangjie/p/4415966.html
Copyright © 2020-2023  润新知