JS-slice()方法返回一个新的数组对象

slice() 方法返回一个新的数组对象,这一对象是一个由 beginend 决定的原数组的浅拷贝(包括 begin,不包括 end)。原始数组不会被改变。

实现strStr() 函数。

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置(从0开始)。如果不存在,则返回-1。
示例1:

1
2
输入:haystack="hello",needle="11"
输出:2

示例2:

1
2
输入:haystack="aaaaa",needle="bbal"
输出:-1

JS代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var strStr = function (haystack, needle) {
if (needle === '') {
return 0;
}
const length = needle.length;
let index = 0;
while (index + length <= haystack.length) {
const str = haystack.slice(index, index + length);
if (str === needle) {
return index;
}
index++;
}
return -1;
}
var haystack = "20190818";
var needle = "1953";

参考文献