28.Implement strStr()

思路:
  • 暴力:
class Solution {
public:
    int strStr(string haystack, string needle) {
        if(needle.length() == 0) return 0;
        bool res = true;
        int len1 = haystack.length();
        int len2 = needle.length();
        for(int i = 0; i <= len1 - len2; i++){
            res = true;
            for(int j = 0; j < needle.length(); j++){
                if(haystack[j+i] != needle[j]) res = false; 
            }
            if(res) return i;
        }
        return -1;
    }
};
  • KMP:待实现。。。
原文地址:https://www.cnblogs.com/UniMilky/p/7025878.html