我的思路: 在haystack中找needle的第一个字母,找到后 haystack 从这个字母开始截取needle.length长度的字符 和 needle比较 如果一致就返回此时的index
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function(haystack, needle) {
let j = 0;
for(let i = 0; i < haystack.length; i++) {
if(needle[0] === haystack[i]) {
if (haystack.substr(i, needle.length) === needle) return i;
}
}
return -1;
};
官方给的题解是kmp,还没有研究明白o(╥﹏╥)o