题解 | #栈的压入、弹出序列#
栈的压入、弹出序列
http://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106
function IsPopOrder(pushV, popV)
{
const stack = new Stack
let j = 0
for(let i = 0 ; i< pushV.length;i++){
stack.push(pushV[i])
//如果当前栈顶和当前出栈的元素一致,则出栈
while(!stack.isEmpty() && stack.peek() == popV[j]){
j++
stack.pop()
}
}
return stack.isEmpty()
}
//这题还是先自己构造一个栈比较方便理解
class Stack{
constructor(){
this.arr = []
}
push = function(item){
this.arr.push(item)
}
pop = function(){
this.arr.pop()
}
peek = function(){
return this.arr[this.arr.length-1]
}
isEmpty = function(){
return this.arr.length == 0
}
}
module.exports = {
IsPopOrder : IsPopOrder
};
