-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path28.ImplementstrStr().h
More file actions
70 lines (52 loc) · 1.45 KB
/
28.ImplementstrStr().h
File metadata and controls
70 lines (52 loc) · 1.45 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
bluepp
2014-06-10
2014-07-11
2014-08-14
May the force be with me!
Problem: Implement strStr()
Source: https://oj.leetcode.com/problems/implement-strstr/
Notes:
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
Solution: Check in the haystack one by one. If not equal to needle, reset the pointer.
*/
/* 2016-09-21, update */
int strStr(string haystack, string needle) {
int m = haystack.size(), n = needle.size();
if (n == 0)
{
return 0;
}
if (m < n)
{
return -1;
}
for (int i = 0; i < m-n+1; i++)
{
int j = 0;
while(j < n && haystack[i+j] == needle[j])
{
j++;
}
if (j == n)
{
return i;
}
}
return -1;
}
char *strStr(char *haystack, char *needle) {
while (true)
{
char *p1 = haystack, *p2 = needle;
while (*p1 != '\0' && *p1 == *p2)
{
p1 ++;
p2++;
}
if (*p2 == '\0') return haystack;
if (*p1 == '\0') return NULL;
haystack ++;
}
}