-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrStr.cc
More file actions
42 lines (39 loc) · 747 Bytes
/
strStr.cc
File metadata and controls
42 lines (39 loc) · 747 Bytes
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
///
/// @file strStr.cc
/// @author majoyz(zmj-miss@live.com)
/// @date 2018-06-20 19:21:53
///
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
int strStr(string haystack, string needle) {
if(needle=="")
return 0;
int lenh = haystack.size();
int lenn = needle.size();
for(int i=0;i<=lenh-lenn;++i){
int tmp=0;
for(int j=0;j<lenn;++j){
if(needle[j]==haystack[i+tmp]){
if(j==lenn-1)
return i;
++tmp;
continue;
}
else
break;
}
}
return -1;
}
};
int main(){
string haystack = "abcdefg";
string needle = "defg";
Solution s;
int answer = s.strStr(haystack,needle);
cout << "answer = " << answer << endl;
return 0;
}