题目:
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) {
if (!*needle) return haystack;
char *p1 = haystack, *p2 = needle;
char *p3 = haystack;
while (*++p2)
p3++;
while (*p3) {
char *beg = p1;
p2 = needle;
while (*p1 && *p2 && *p1 == *p2) {
p1++;
p2++;
}
if (!*p2)
return beg;
p1 = beg + 1;
p3++;
}
return NULL;
}
};