-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImplementstrStr().cpp
34 lines (32 loc) · 1.01 KB
/
ImplementstrStr().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
// Source: https://leetcode.com/problems/implement-strstr/
// 2015/4/27
// My Soultion1
// Using string class find function
class Solution {
public:
int strStr(string haystack, string needle) {
auto pos = haystack.find(needle);
return (pos == string::npos) ? -1 : pos;
}
}; // 6ms
// My Soultion2
class Solution {
public:
int strStr(string haystack, string needle) {
if (needle.empty()) return 0;
if (haystack.size() < needle.size()) return -1;
int pos = -1;
for (int i = 0; i < (haystack.size()-needle.size()+1);++i) {
if (haystack[i] == needle[0]) {
int j = 1, tmp_pos = i;
for (int ix = i + 1; j != needle.size() && ix != haystack.size(); ++ix,++j)
if (haystack[ix] != needle[j]) break;
if (j == needle.size()) {
pos = tmp_pos;
break;
}
}
}
return pos;
}
}; // 8ms