题解 | #【模板】栈#

【模板】栈

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;
let arr = [];

void async function () {
    // Write your code here
    while(line = await readline()){
        let tokens = line.split(' ');
        let len = arr.length;
        if(tokens[0] == 'push') {
          arr.push(parseInt(tokens[1]));
        }else if(tokens[0] == 'pop') {
          if(arr[len - 1]) {
            console.log(arr[len - 1]);
            arr.pop()
          }else {
            console.log("error");
          }
        }else if(tokens[0] == "top") {
          if(arr[len - 1]) {
            console.log(arr[len - 1]);
          }else {
            console.log("error");
          }
        }
     }
}()

// 第一次做牛客的题目,感觉不是很顺手,不知道用户输入什么需要用什么获取,后面多做题吧!!!

算法题合集 文章被收录于专栏

自己做算法的解题代码

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-03 16:22
点赞 评论 收藏
分享
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-03 17:37
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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