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

栈的压入、弹出序列

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
};
全部评论

相关推荐

11-05 10:55
中南大学 Java
要双修的猫头鹰:这面试官怕不是个m
我来点评面试官
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务