题解 | #栈的压入、弹出序列#

栈的压入、弹出序列

http://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106

function IsPopOrder(pushV, popV) {
  if (!pushV || !popV || pushV.length !== popV.length) {
    return;
  }
  const stack = new Stack();
  let j = 0;
  for (let i = 0; i < pushV.length; i++) {
    stack.push(pushV[i]); //遍历push栈
    while (!stack.isEmpty() && stack.peek() == popV[j]) {
      stack.pop(); //相等则将stack辅助栈栈顶弹出,并将pop栈的index移动
      j++;
    }
  }
  return stack.isEmpty();
}
class Stack {
  constructor() {
    this.arr = [];
  }
  push(item) {
    this.arr.push(item);
  }
  pop() {
    this.arr.pop();
  }
  isEmpty() {
    return this.arr.length === 0;
  }
  peek() {
    return this.arr[this.arr.length - 1];
  }
}
module.exports = {
  IsPopOrder: IsPopOrder,
};

全部评论

相关推荐

运营3年修炼中接简历辅导:你的科研项目经历里,只写了你的动作,没有写你的思考和成果,不要只写使用什么进行了什么,这等于罗列你的任务,简历是为了突出你的优秀,你在什么样的任务背景下,克服了什么样的困难,针对性地做了哪些事情,最后达成了什么成果(用数据体现你的成果和效率)
点赞 评论 收藏
分享
ResourceUtilization:四六级不愧是大学最有用的证之一
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务