题解 | #【模板】栈#

【模板】栈

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

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
class Stack{
    constructor(x){
        this.x=x;
        this.arr=[]
    }
    push(x){
        return !isNaN(parseInt(x)) ? this.arr.push(x):console.log("输入整数")
    }
    pop(){
        return this.arr.length===0 ? 'error':this.arr.pop();
    }
    top(){
        let length=this.arr.length
        return this.arr.length===0 ? 'error':this.arr[length-1]
    }
}
let stack=new Stack()
let count=1;
let dataLine=[]
let n
void async function () {
    // Write your code here
    while(line = await readline()){
        dataLine.push(line)
        if(count==1){
            n=parseInt(dataLine[0])
        }
        
    }
    for(let i=1;i<=n;i++){
        if(dataLine[i].includes('push')){
            stack.push(dataLine[i].split(' ')[1])
        }else if(dataLine[i].includes('pop')){
            let numPop=stack.pop()
            console.log(numPop)
        }else if(dataLine[i].includes('top')){
            let numTop=stack.top()
            console.log(numTop)
        }
    }
}()

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-01 17:13
想去,但是听说加班强度实在难崩,所以拒绝了,现在有点心梗对面hr感觉也是实习生,打电话的时候怪紧张的,但是感觉人很好嘞
水中水之下水道的鼠鼠:哥们这不先去体验一下,不行再跑呗,大不了混个实习经历(有更好的转正offer就当我没说)
点赞 评论 收藏
分享
深夜书店vv:腾讯是这样的,去年很多走廊都加桌子当工区
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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