Skip to content

Latest commit

 

History

History
40 lines (33 loc) · 1.03 KB

0459.重复的子字符串.md

File metadata and controls

40 lines (33 loc) · 1.03 KB

题目地址

https://leetcode-cn.com/problems/repeated-substring-pattern/

思路

C++代码

class Solution {
public:
    void preKmp(int* next, const string& s){
        next[0] = -1;
        int j = -1;
        for(int i = 1;i < s.size(); i++){
            while(j >= 0 && s[i] !=s [j+1])
                j = next[j];
            if(s[i] == s[j+1])
                j++;
            next[i] = j;
        }
    }
    bool repeatedSubstringPattern(string s) {
        if (s.size() == 0) {
            return false;
        }
        int next[s.size()];
        preKmp(next, s);
        int len = s.size();
        if (next[len - 1] != -1 && len % (len - (next[len - 1] + 1)) == 0) {
            return true;
        }
        return false;

    }
};

笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 GitHubhttps://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。