• LeetCode 每日一题(5. 最长回文子串)


    class Solution {
    public:
        string longestPalindrome(string s) {
            if (s.size() <= 1){
                return s;
            }
    
            int a = 0;  //记录临时值
            int length = 0;
    
            int ta = 1; //滑动窗口边界
            int tb = 0;
    
            int t = 0;
    
            bool flag = false;  //匹配标记
    
            const char* p = s.c_str();
            for(int i=0;i<s.size();i++){            
                for(int j = i + length;j<s.size();j++){
                    t = (j-i+1);                
                    tb = t/2;
    
                    for(int k =i;k<(i+tb);k++){
                        if (p[k] != p[j - k + i]) {
                            flag = true;
                            break;
                        }
                    }
                    
                    if (!flag){
                        if (t > length){
                            a = i;
                            length = t;
                        }
                    }
                    flag = false;
                }
            }
    
            if (a == 0 && length == 0) {
                length = 1;
            }
            return s.substr(a,length);
        }
    };
  • 相关阅读:
    Examples
    Examples
    Examples
    Examples
    Examples
    Examples
    Examples
    Examples
    Examples
    如何把随机数或者对象添加到ArrayList集合
  • 原文地址:https://www.cnblogs.com/lesten/p/14961273.html
Copyright © 2020-2023  润新知