leetcode-Implement strStr()

来源:互联网 发布:mac怎么下载穿越火线 编辑:程序博客网 时间:2024/06/04 19:12

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) {        char *p1 = haystack;        char *p2 = needle;        if(*p2 == 0)return p1;        int n = strlen(p2);        int m = strlen(p1);        if(n > m)return NULL;        while(*p1 != 0)        {            if(*p1 != *p2)p1++;            if(strncmp(p1,p2,n) == 0)            {                return p1;            }            p1++;        }        return NULL;    }};


0 0