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)$

相关文章:

  • 2021-11-01
  • 2021-06-24
  • 2021-07-25
  • 2021-12-25
  • 2021-11-28
  • 2021-08-08
  • 2022-12-23
  • 2021-12-18
猜你喜欢
  • 2022-02-02
  • 2021-06-02
  • 2021-04-24
  • 2021-07-02
  • 2021-10-18
  • 2021-04-27
相关资源
相似解决方案