-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubstring_match.cpp
55 lines (51 loc) · 1.54 KB
/
substring_match.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle
字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack
的一部分,则返回 -1 。
提示:
:
1 <= haystack.length, needle.length <= 104
haystack 和 needle 仅由小写英文字符组成
*/
/* 解题思路:
1.如果haystack.length < needle.length,则返回-1
2.用双指针遍历haystack
3.left记录等于needle一个字符的索引,
4.right向右扩展直到出现不等于needl的字符,如果再次遇到needle第一个字符,
则更新left的值
5.如果left往后needle.length个范围的字符和needl相同,则返回left
*/
#include <iostream>
using namespace std;
// 这里采用了暴力匹配法(O(N∗M)),推荐学习KMP算法可以大幅降低时间复杂度为线性
int strStr(string haystack, string needle)
{
int str_len{static_cast< int >(haystack.length())};
int patt_len{static_cast< int >(needle.length())};
for (int i = 0; i < str_len; ++i)
{
if (str_len - i >= patt_len)
{
int temp_i{i};
for (int j = 0; j < patt_len; ++j)
{
if (haystack[temp_i++] != needle[j])
{
break;
}
if (j == patt_len - 1)
{
return i;
}
}
}
}
return -1;
}
int main()
{
string haystack = "leetcode";
string needle = "leeto";
cout << strStr(haystack, needle);
return 0;
}