【LeetCode刷题】- 实现strStr()

Task:

【LeetCode刷题】- 实现strStr()

代码:

class Solution {
public:
    int strStr(string haystack, string needle) {
        if(needle.size() == 0)
            return 0;
        for(int i = 0; i < haystack.size(); i++){
            if(i+needle.size()-1 >= haystack.size())
                return -1;
            int flag = 1;
            for(int j = 0; j <needle.size(); j++){
                if (haystack[i+j] == needle[j])
                    continue;
                flag = 0;
            }
            if (flag == 1)
                return i;
        }
        return -1;
        
    }
};

提交结果:

【LeetCode刷题】- 实现strStr()

;