s4553711
7/11/2017 - 3:54 PM

28.cpp

class Solution {
public:
    int strStr(string haystack, string needle) {
        if (haystack.size() == 0 && needle.size() == 0) return 0;
        if (needle.size() == 0)  return 0;
        if (haystack.size() < needle.size()) return -1;
        
        for(int i = 0; i < (haystack.size() - needle.size() + 1); i++) {
            bool flag = true;
            if (needle[0] == haystack[i]) {
                int j = 0;
                for(;j < needle.size(); j++) {
                    if (needle[j] != haystack[i+j]) {
                        flag = false;
                        break;
                    }
                }
                if (flag) return i;
            }
        }
        return -1;
    }
};