Skip to content

Commit

Permalink
面试题手写数组方法
Browse files Browse the repository at this point in the history
  • Loading branch information
weilaiqishi committed Jul 24, 2022
1 parent 0fe3ce0 commit 90ff53c
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 40 deletions.
13 changes: 0 additions & 13 deletions 前端/1html&js/jscode/array/map.js

This file was deleted.

38 changes: 38 additions & 0 deletions 前端/1html&js/jscode/array/map.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 实现数组map方法

原文 <https://juejin.cn/post/6844903986479251464#heading-25>

依照 [ecma262 草案](https://tc39.es/ecma262/#sec-array.prototype.map),实现的map的规范如下:

![ecma262 Array.map](https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2019/11/3/16e311d99e860405~tplv-t2oaga2asx-zoom-in-crop-mark:3024:0:0:0.awebp)

```js
Array.prototype.map = function(callbackFn, thisArg) {
// 处理数组类型异常
if (this === null || this === undefined) {
throw new TypeError("Cannot read property 'map' of null or undefined");
}
// 处理回调类型异常
if (Object.prototype.toString.call(callbackfn) != "[object Function]") {
throw new TypeError(callbackfn + ' is not a function')
}
// 草案中提到要先转换为对象
let O = Object(this);
let T = thisArg;

let len = O.length >>> 0; // 字面意思是指"右移 0 位",但实际上是把前面的空位用0填充,这里的作用是保证len为数字且为整数。
let A = new Array(len);
for(let k = 0; k < len; k++) {
// 还记得原型链那一节提到的 in 吗?in 表示在原型链查找
// 如果用 hasOwnProperty 是有问题的,它只能找私有属性
// 如果没有找到就不处理,能有效处理稀疏数组的情况。
if (k in O) {
let kValue = O[k];
// 依次传入this, 当前项,当前索引,整个数组
let mappedValue = callbackfn.call(T, KValue, k, O);
A[k] = mappedValue;
}
}
return A;
}
```
42 changes: 42 additions & 0 deletions 前端/1html&js/jscode/array/push pop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 实现数组 push、pop 方法

原文 <https://juejin.cn/post/6844903986479251464#heading-27>

参照 ecma262 草案的规定,关于 push 和 pop 的规范如下图所示:

![push](https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2019/11/3/16e311f4fa483cc2~tplv-t2oaga2asx-zoom-in-crop-mark:3024:0:0:0.awebp)
![pop](https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2019/11/3/16e311fa338c2ecb~tplv-t2oaga2asx-zoom-in-crop-mark:3024:0:0:0.awebp)

```js
Array.prototype.push = function(...items) {
let O = Object(this);
let len = this.length >>> 0;
let argCount = items.length >>> 0;
// 2 ** 53 - 1 为JS能表示的最大正整数
if (len + argCount > 2 ** 53 - 1) {
throw new TypeError("The number of array is over the max value restricted!")
}
for(let i = 0; i < argCount; i++) {
O[len + i] = items[i];
}
let newLength = len + argCount;
O.length = newLength;
return newLength;
}
```

```js
Array.prototype.pop = function() {
let O = Object(this);
let len = this.length >>> 0;
if (len === 0) {
O.length = 0;
return undefined;
}
len --;
let value = O[len];
delete O[len];
O.length = len;
return value;
}
```
18 changes: 0 additions & 18 deletions 前端/1html&js/jscode/array/reduce.js

This file was deleted.

49 changes: 49 additions & 0 deletions 前端/1html&js/jscode/array/reduce.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 实现数组reduce方法

原文 <https://juejin.cn/post/6844903986479251464#heading-26>

依照 [ecma262 草案](https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2019/11/3/16e311ed2bfa8fad~tplv-t2oaga2asx-zoom-in-crop-mark:3024:0:0:0.awebp),实现的reduce的规范如下:

其中有几个核心要点:

1、初始值不传怎么处理
2、回调函数的参数有哪些,返回值如何处理。

```js
Array.prototype.reduce = function(callbackfn, initialValue) {
// 异常处理,和 map 一样
// 处理数组类型异常
if (this === null || this === undefined) {
throw new TypeError("Cannot read property 'reduce' of null or undefined");
}
// 处理回调类型异常
if (Object.prototype.toString.call(callbackfn) != "[object Function]") {
throw new TypeError(callbackfn + ' is not a function')
}
let O = Object(this);
let len = O.length >>> 0;
let k = 0;
let accumulator = initialValue;
if (accumulator === undefined) {
for(; k < len ; k++) {
// 查找原型链
if (k in O) {
accumulator = O[k];
k++;
break;
}
}
}
// 表示数组全为空
if(k === len && accumulator === undefined)
throw new Error('Each element of the array is empty');
console.log(k)
for(;k < len; k++) {
if (k in O) {
// 注意,核心!
accumulator = callbackfn.call(undefined, accumulator, O[k], k, O);
}
}
return accumulator;
}
```
20 changes: 11 additions & 9 deletions 前端/1html&js/算法/0核心框架/0_BFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
// 将s[j]向上拨动一次
function plusOne(str, index) {
const arr = str.split('')
arr[index] = arr[index] == 9 ? 0 : arr[index] - 0 + 1
const num = Number(arr[index])
arr[index] = num === 9 ? 0 : num + 1
return arr.join('')
}
// 将s[j]向下拨动一次
function minusOne(str, index) {
const arr = str.split('')
arr[index] = arr[index] == 0 ? 9 : arr[index] - 1
const num = Number(arr[index])
arr[index] = num === 0 ? 9 : arr[index] - 1
return arr.join('')
}

Expand Down Expand Up @@ -74,7 +76,6 @@ function minusOne(str, index) {
// 双向BFS Set 740 ms 52.8 MB
const openLock = function (deadends, target) {
if (deadends.includes(target)) { return -1 }
const deads = new Set(deadends)
const q = []
const visited = new Set()
// 从起点开始启动广度优先搜索
Expand All @@ -91,11 +92,13 @@ const openLock = function (deadends, target) {
while (q.length && q1.length) {
/* 判断是否到达终点 */
if (q.some(item => q1.includes(item))) {
return step - 0 + step1
return step + step1
}
if (q.some(item => item === target)) { return step }
step = run(deadends, q, visited, step)
step1 = run(deadends, q1, visited1, step1)
run(deadends, q, visited, step)
run(deadends, q1, visited1, step1)
step++
step1++
}
return -1
}
Expand All @@ -120,9 +123,8 @@ function run(deadends, q, visited, step) {
})
}
}
return step + 1
}

console.log(openLock(["0201", "0101", "0102", "1212", "2002"], "0202"), 6)
// console.log(openLock(["0000"], "8888"), -1)
// console.log(openLock(["8888"], "0009"), 1)
console.log(openLock(["0000"], "8888"), -1)
console.log(openLock(["8888"], "0009"), 1)

0 comments on commit 90ff53c

Please sign in to comment.