Implement strStr()

my solution

class Solution { 
public:
    int strStr(string haystack, string needle) {
        // "" ""
        // "ewe" ""
        // "" "ewe"

        //"dddd" "ff"
        //"abc" "bc"
        //"f" "fffffffff"

        int len_hay = haystack.length();
        int len_needle = needle.length();

        if(len_needle == 0){
            return 0;
        }

        if(len_hay == 0 || len_needle == 0 || len_needle > len_hay){
            return -1;
        }

        for(int i=0; i<len_hay;i++){
            if((i+len_needle-1) > len_hay -1)
                return -1;

            for(int j = 0; j<len_needle;j++){ 
                if(needle[j] != haystack[i+j]) break;
                if(j == len_needle -1) return i;
            }
        }

        return -1;
    }
};

excellent answer

/**
 * 本代码由九章算法编辑提供。没有版权欢迎转发。
 * - 九章算法致力于帮助更多中国人找到好的工作,教师团队均来自硅谷和国内的一线大公司在职工程师。
 * - 现有的面试培训课程包括:九章算法班,系统设计班,BAT国内班
 * - 更多详情请见官方网站:http://www.jiuzhang.com/
 */

class Solution {
public:
    int strStr(string haystack, string needle) {
        int i, j, lenh = haystack.length(), lenn = needle.length();
        if (lenn == 0)  return 0;
        for (i = 0; i <= lenh - lenn; i++) {
            for (j = 0; j < lenn; j++) 
                if (haystack[i + j] != needle[j]) break;
            // 匹配成功
            if (j == lenn)  return i;
        }
        return -1;
    }
};