28. Implement strStr()

来源:互联网 发布:linux c list用法 编辑:程序博客网 时间:2024/05/29 06:58
class Solution {public:    int strStr(string haystack, string needle)     {        int nl = needle.size(), hl = haystack.size(), ret = 0;                if(nl == 0)            return 0;        if(hl == 0)            return -1;                for(; ret < hl - nl + 1; ret++)        {            if(haystack.at(ret) == needle.at(0) && haystack.substr(ret, nl) == needle)                return ret;        }        return -1;    }};



class Solution {public:    int strStr(string haystack, string needle)     {        return haystack.find(needle, 0);    }};


原创粉丝点击