LeetCode-Implement strStr()

来源:互联网 发布:兴业银行淘宝白金卡 编辑:程序博客网 时间:2024/05/23 02:01

Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

Solution:

Code:

<span style="font-size:14px;">class Solution {public:    char *strStr(char *haystack, char *needle) {        const int lengthH = strlen(haystack);        const int lengthN = strlen(needle);        for (int i = 0; i <= lengthH-lengthN; ++i) {            bool match = true;            for (int j = 0; j < lengthN; ++j)                if (haystack[i+j] != needle[j]) {                    match = false;                    break;                }            if (match) return &haystack[i];        }        return NULL;    }};</span>



0 0
原创粉丝点击