题解 | #【模板】栈#

【模板】栈

http://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf

let Stack = []
let top = -1
while(n--){
    let arr = readline().split(" ")
    if(arr.length === 2){
        Stack[++top] = arr[1]
    }else if(arr[0] === "pop"){
        if(top === -1){  //栈为空
            console.log("error")
        }else{
            console.log(Stack[top--])
        }
    }else{  //top
        if(top === -1){  //栈为空
            console.log("error")
        }else{
            console.log(Stack[top])
        }
    }
}
全部评论

相关推荐

评论
点赞
1
分享

创作者周榜

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