LeetCode Day24 implement-strstr

class Solution {
public:
    int strStr(string haystack, string needle) {
        if(!needle.size())return 0;
        int m = haystack.size(), n = needle.size();
        if (m < n) return -1;
        for(int i=0;i<=m-n;i++){
            int j=0;
            for(j=0;j<n;j++){
                if(haystack[i+j]!=needle[j]) break;
            }
            //printf("%d",nn);
            if(j==n) return i;
        }
        return -1;
    }
};

KMP算法
参考从头到尾彻底理解KMP

猜你喜欢

转载自blog.csdn.net/weixin_41394379/article/details/83241524