实现 strStr() - lifengyu360/lifengyu_first_git_test GitHub Wiki

class Solution {

public:

int strStr(string haystack, string needle) {
    if (needle.empty()){
        return 0;
    }

    for (int i = 0; i < haystack.length(); i++){
        int index = i;
        if ((haystack.length() - index) < needle.length() ) {
            break;  
        }
        bool b_sucess = true;
        for (int j = 0; j < needle.length(); j++){
            if (needle[j] == haystack[index]){
                index++;
                continue;
            }
            b_sucess = false;
            break;
        }
        if (b_sucess) return i;
    }

    return -1;
}

};