-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain28.cpp
35 lines (33 loc) · 1.01 KB
/
main28.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
class Solution {
public:
int strStr(string haystack, string needle) {
if (!needle.size())
return 0;
if (!haystack.size())// || needle.size() > haystack.size())
return -1;
int i = 0, j = 0;
while (i < int(haystack.size() - needle.size() + 1))
//haystack.size() - needle.size() + 1是无符号整型,
//有符号与无符号比较,自动把有符号转换成无符号
{
//cout << i << ' ' << haystack.size() << ' ' << needle.size() << ' ' << haystack.size() - needle.size() + 1 << endl;
while (j < needle.size())
{
if (haystack[i] != needle[j])
{
i = i - j + 1;
j = 0;
break;
}
else
{
++i;
++j;
}
}
if (j == needle.size())
return i - j;
}
return -1;
}
};