Implement strStr()

暴力法即可。不需要用kmp,太复杂了。

    char *strStr(char *haystack, char *needle) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        if(!needle)
            return haystack;
        if(!haystack)
            return NULL;
        
        int len1 = strlen(haystack);
        int len2 = strlen(needle);
        
        for(int i=0;i<=(len1-len2);i++)
        {
            char *p = haystack+i;
            char *q = needle;
            while(*p!='\0'&&*q!='\0'&&*p==*q)
            {
                p++;
                q++;
            }
            if(*q=='\0')
                return haystack+i;
        }
        return NULL;
    }

  

posted @ 2013-10-22 15:47  summer_zhou  阅读(159)  评论(0)    收藏  举报