forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
38 lines (37 loc) · 917 Bytes
/
solution.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
// class Solution
// {
// public:
// int strStr(string haystack, string needle)
// {
// if(needle.size() == 0) return 0;
// for(int i=0;i<haystack.size();i++)
// {
// int j = 0;
// for(j;j<needle.size();j++)
// {
// if(haystack[i+j] != needle[j])
// break;
// }
// if(j == needle.size())
// return i;
// }
// return -1;
// }
// };
class Solution {
public:
int strStr(string haystack, string needle)
{
int m = haystack.length(), n = needle.length();
if (!n) return 0;
for (int i = 0; i < m - n + 1; i++)
{
int j = 0;
for (; j < n; j++)
if (haystack[i + j] != needle[j])
break;
if (j == n) return i;
}
return -1;
}
};