题解 | #【模板】栈#

【模板】栈

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");
          }
        }
     }
}()

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

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

自己做算法的解题代码

全部评论

相关推荐

沟头学院:无关比赛不要写,这样会显着你主次不分,比赛不要撒谎,有哪些就写那些,创新创业建议删除。技能特长可以适当夸大。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务