-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
49 lines (47 loc) · 944 Bytes
/
main.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
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
// 字符串匹配,暴力匹配
class Solution {
public:
int strStr(string haystack, string needle) {
int n = haystack.size(), m = needle.size();
for(int i = 0; i <= n-m; ++i)
{
int j = 0;
for(;j < m; ++j)
{
if(haystack[i+j] != needle[j])
break;
}
if(j == m)
return i;
}
return -1;
}
};
/*
class Solution {
public:
int strStr(string haystack, string needle) {
if(haystack.find(needle) != string::npos)
{
return haystack.find(needle);
}
else
{
return -1;
}
}
};
*/
int main()
{
string str1 = "hello";
string str2 = "ll";
Solution s;
cout << s.strStr(str1, str2) << endl;
return 0;
}