题解 | #栈的压入、弹出序列#
栈的压入、弹出序列
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,
};

查看6道真题和解析