[leetcode] 28. Implement strStr()

来源:互联网 发布:国家数据平台 编辑:程序博客网 时间:2024/06/06 09:37

思路

找一个字符串中是否包含另一个字符串,并返回其位置。按照复杂的做那么就是KMP

class Solution {public:    int strStr(string haystack, string needle) {        int i = -1,len1 = haystack.size(),len2 = needle.size();        while(++i <= len1-len2){            string tem = haystack.substr(i,len2);            if(tem == needle)                return i;        }        return -1;    }};
原创粉丝点击