forked from leetcoders/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementstrStr().h
More file actions
31 lines (29 loc) · 843 Bytes
/
ImplementstrStr().h
File metadata and controls
31 lines (29 loc) · 843 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
/*
Author: Annie Kim, anniekim.pku@gmail.com
Date: Apr 9, 2013
Update: Jul 19, 2013 (Refactor)
Problem: Implement strStr()
Difficulty: Easy
Source: http://leetcode.com/onlinejudge#question_28
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.
*/
class Solution {
public:
char *strStr(char *haystack, char *needle) {
while(true)
{
char *h = haystack, *n = needle;
while (*n != '\0' && *h == *n)
{
h++;
n++;
}
if (*n == '\0') return haystack;
if (*h == '\0') return NULL;
haystack++;
}
}
};