LeetCode Implement strStr()

来源:互联网 发布:淘宝怎么制作图片 编辑:程序博客网 时间:2024/06/05 05:20

题目:

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;}};


0 0
原创粉丝点击