Mr ToPu
Mr ToPu
```js /** * @param {string} haystack * @param {string} needle * @return {number} */ var strStr = function (haystack, needle) { let h1 = 0 let n1 = 0 while...
*不知道是不是这个意思* ```js // 构建树 class TreeNode { constructor (val = 0, left = null, right = null) { this.val = val this.left = left this.right = right } } function...
```javascript class Scheduler { constructor (max) { // 最大并发量 this.max = max // 当前并发数 this.count = 0 // 执行队列 this.queue = [] // 是否可以运行 this.isRunning = true } // 添加任务...
```javascript // 使用调度器实现,控制并发为1 class Scheduler { constructor (maxTask) { // 最大并发量 this.maxTask = maxTask // 任务队列 this.queue = [] // 当前执行任务的数量 this.currentTask = 0 } // 添加任务 addTask (task) {...
```javascript /** * new Quene() * .task(1000, () => { * console.log(1) * }) * .task(2000, () => { * console.log(2) * }) * .task(1000, () => { * console.log(3)...
```js // 工厂类 class shapeFactory { chooseShape (type) { if (type === 'circle') { return new Circle() } if (type === 'rectangle'){ // ... } } } // 圆形类 class...
```javascript /** * 请求超过4s自动失败 * @returns {Promise} */ function getData () { return Promise.race([ new Promise((resolve, reject) => { // 模拟一个6s的请求 setTimeout(() => { resolve('请求成功') }, 6000) }), new Promise((resolve,...
```js /** * 原地打乱数组(洗牌算法) */ function shuffle (arr) { for (let i = 0; i < arr.length; i++) { let j = Math.floor(Math.random() * i); [arr[i], arr[j]] = [arr[j], arr[i]]...
```javascript /** * Promise.retry当请求失败的时候,重新发起请求,直到请求成功 * @param fn * @param time 重新请求的次数 * @param delay 重启的延迟时间 * @returns {Promise} * @private */ Promise._retry = function (fn, time, delay) { let count...
```js function getSum (a, b) { // 进位 let curry = 0 let maxLength = Math.max(a.length, b.length) a = a.padStart(maxLength, 0) b = b.padStart(maxLength, 0) let res = '' for...