function strStr(haystack, needle) {
if (needle === '') return 0
// js已有轮子
// return haystack.indexOf(needle)
// 相当于自己实现一个indexOf方法
for (let i = 0; i < haystack.length; i++) {
if (haystack[i] === needle[0]) {
if (haystack.slice(i, i + needle.length) === needle) {
console.log(i)
return i
}
}
}
return -1
}
leetCode -实现一个strStr(),返回一个字符串在另一个字符串中出现的位置
最新推荐文章于 2022-10-23 22:39:35 发布