使用双指针暴力解决力扣28题《实现 strStr()》

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

时间复杂度 $O(M*N)$

posted @ 2020-03-24 15:33  至尊王者  阅读(201)  评论(0编辑  收藏  举报