算法题

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) {
    1. return false
    } this.con = this.con.concat(x) return this.con };

/**

  • @return {number} */ CustomStack.prototype.pop = function() { if (this.con.length<=0) {
    1. return -1
    } let tmp = this.con.slice(this.con.length-1,this.con.length); this.con = this.con.slice(0,this.con.length-1); return tmp };

/**

  • @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++) {
    1. this.con[i] += val
    } return this.con }; ```
  • 同学的解法,比我的内存使用率低 ```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) {
      1. return false
      } this.con[this.top++] = x };

/**

  • @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++) {
    1. this.con[i] += val
    } return this.con };
  1. <a name="wW3UY"></a>
  2. ### 编程题
  3. - 题目:
  4. - 链接:[https://bigfrontend.dev/zh/problem/implement-Promise-allSettled](https://bigfrontend.dev/zh/problem/implement-Promise-allSettled)
  5. - 和Promise.all()不同,Promise.allSettled() 会等待所有的promise直到fulfill或者reject。
  6. - 你能实现自己的Promise.allSettled() 吗?
  7. - 答案
  8. ```javascript
  9. /**
  10. * @param {Array<any>} promises - notice that input might contains non-promises
  11. * @return {Promise<Array<{status: 'fulfilled', value: any} | {status: 'rejected', reason: any}>>}
  12. */
  13. function allSettled(promises) {
  14. // your code here
  15. if(promises.length===0) return Promise.resolve([])
  16. let _promises = promises.map(item=>{
  17. return item instanceof Promise?item:Promise.resolve(item)
  18. })
  19. return new Promise((resolve,reject) =>{
  20. const result = []
  21. let promiseNum = _promises.length
  22. _promises.forEach((promise,index)=>{
  23. promise.then(value=>{
  24. result[index] = {
  25. status:'fulfilled',
  26. value
  27. }
  28. promiseNum -= 1
  29. if (promiseNum === 0) {
  30. resolve(result)
  31. }
  32. },(reason)=>{
  33. result[index] = {
  34. status:'rejected',
  35. reason
  36. }
  37. promiseNum -= 1
  38. if (promiseNum === 0) {
  39. resolve(result)
  40. }
  41. })
  42. })
  43. })
  44. }
  • 特性
    • 参数 :是个数组,数组的每一项都是一个promise
    • 返回值:
      • 返回一个在所有给定的promise 都已经fulfilled或者rejected后的promise,并带有一个对象数组,每个对象表示对应的promise结果。
      • 对于每个对象结果,都有一个status字符串,如果他的值为fulfilled,则结果对象上存在一个value.如果值为rejected,则存在一个reason。 value 或者reason反映了每个promise 决议的值