forked from gulinghao1847/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanswer.cpp
More file actions
30 lines (28 loc) · 873 Bytes
/
answer.cpp
File metadata and controls
30 lines (28 loc) · 873 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
/*
Implement strStr() Total Accepted: 5346 Total Submissions: 26470 My Submissions
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
*/
class Solution {
public:
char *strStr(char *haystack, char *needle) {
int s1 = 0;
int s2 = 0;
while(haystack[s1] != '\0') s1++;
while(needle[s2] != '\0') s2++;
if(s1 == 0 && s2 == 0) return needle;
if(s2 == 0) return haystack;
for(int i = 0; i < s1 - s2 + 1; i++){
if(haystack[i] == needle[0]){
int count = 0;
while(haystack[i + count] == needle[count]){
count++;
if(count == s2){
return &haystack[i];
}
}
}
}
return NULL;
}
};