算法题
https://leetcode-cn.com/problems/design-a-stack-with-increment-operation/
- 请你设计一个支持下述操作的栈。
- 实现自定义栈类 CustomStack :
- CustomStack(int maxSize):用 maxSize 初始化对象,maxSize 是栈中最多能容纳的元素数量,栈在增长到 maxSize 之后则不支持 push 操作。
- void push(int x):如果栈还未增长到 m axSize ,就将 x 添加到栈顶。
- int pop():弹出栈顶元素,并返回栈顶的值,或栈为空时返回 -1 。
- void inc(int k, int val):栈底的 k 个元素的值都增加 val 。如果栈中元素总数小于 k ,则栈中的所有元素都增加 val 。
- 我的解法,不断操作con,所以内存使用率高
```javascript
/**
- @param {number} maxSize */ var CustomStack = function(maxSize) { this.maxSize = maxSize this.con = [] };
/**
- @param {number} x
- @return {void}
*/
CustomStack.prototype.push = function(x) {
if (this.con.length>=this.maxSize) {
} this.con = this.con.concat(x) return this.con };return false
/**
- @return {number}
*/
CustomStack.prototype.pop = function() {
if (this.con.length<=0) {
} let tmp = this.con.slice(this.con.length-1,this.con.length); this.con = this.con.slice(0,this.con.length-1); return tmp };return -1
/**
- @param {number} k
- @param {number} val
- @return {void}
*/
CustomStack.prototype.increment = function(k, val) {
let len = this.con.length>k ? k : this.con.length
for (let i=0;i<len;i++) {
} return this.con }; ```this.con[i] += val
- 同学的解法,比我的内存使用率低
```javascript
/**
- @param {number} maxSize / var CustomStack = function(maxSize) { this.maxSize = maxSize this.con = [] this.top = 0 }; /*
- @param {number} x
- @return {void}
*/
CustomStack.prototype.push = function(x) {
if (this.top>=this.maxSize) {
} this.con[this.top++] = x };return false
/**
- @return {number} */ CustomStack.prototype.pop = function() { return this.top?this.con[—this.top]:-1 };
/**
- @param {number} k
- @param {number} val
- @return {void}
*/
CustomStack.prototype.increment = function(k, val) {
let len = this.top>k ? k : this.top
for (let i=0;i<len;i++) {
} return this.con };this.con[i] += val
<a name="wW3UY"></a>
### 编程题
- 题目:
- 链接:[https://bigfrontend.dev/zh/problem/implement-Promise-allSettled](https://bigfrontend.dev/zh/problem/implement-Promise-allSettled)
- 和Promise.all()不同,Promise.allSettled() 会等待所有的promise直到fulfill或者reject。
- 你能实现自己的Promise.allSettled() 吗?
- 答案
```javascript
/**
* @param {Array<any>} promises - notice that input might contains non-promises
* @return {Promise<Array<{status: 'fulfilled', value: any} | {status: 'rejected', reason: any}>>}
*/
function allSettled(promises) {
// your code here
if(promises.length===0) return Promise.resolve([])
let _promises = promises.map(item=>{
return item instanceof Promise?item:Promise.resolve(item)
})
return new Promise((resolve,reject) =>{
const result = []
let promiseNum = _promises.length
_promises.forEach((promise,index)=>{
promise.then(value=>{
result[index] = {
status:'fulfilled',
value
}
promiseNum -= 1
if (promiseNum === 0) {
resolve(result)
}
},(reason)=>{
result[index] = {
status:'rejected',
reason
}
promiseNum -= 1
if (promiseNum === 0) {
resolve(result)
}
})
})
})
}
- 特性
- 参数 :是个数组,数组的每一项都是一个promise
- 返回值:
- 返回一个在所有给定的promise 都已经fulfilled或者rejected后的promise,并带有一个对象数组,每个对象表示对应的promise结果。
- 对于每个对象结果,都有一个status字符串,如果他的值为fulfilled,则结果对象上存在一个value.如果值为rejected,则存在一个reason。 value 或者reason反映了每个promise 决议的值