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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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)$