티스토리 뷰

https://www.acmicpc.net/problem/10773

 

10773번: 제로

첫 번째 줄에 정수 K가 주어진다. (1 ≤ K ≤ 100,000) 이후 K개의 줄에 정수가 1개씩 주어진다. 정수는 0에서 1,000,000 사이의 값을 가지며, 정수가 "0" 일 경우에는 가장 최근에 쓴 수를 지우고, 아닐 경

www.acmicpc.net

const fs = require('fs');
const [n, ...command] = fs.readFileSync("./dev/stdin").toString().trim().split("\n").map(v=>+v);


class Node{
    constructor(item){
      this.item = item;
      this.next = null;
    }
  }
  
  class Stack{
    constructor(){
      this.topOfStack = null;
      this.length = 0;
    }
  
    push(item){
      const node = new Node(item);
      if(this.topOfStack!=null){
        node.next = this.topOfStack; 
      }
        this.topOfStack = node;
        this.length+=1;
    }
  
    pop(){
      if(this.length==0)return -1;
      const popItem = this.topOfStack;
      this.topOfStack = popItem.next;
      this.length-=1;

      return popItem.item
    }
  
    size(){
      return this.length;
    }
  
    empty(){
      if(this.length==0) return 1;
      else return 0;
    }
  
    top(){
      if(this.length==0)return -1;
      return this.topOfStack.item; 
    }
  
  }


let answer = [];
let stack = new Stack(); 

command.forEach(v=>{
    if(v==0){
        stack.pop();
    }else{
        stack.push(v)
    }
})


while(stack.length!=0){
    answer.push(stack.pop());
}
console.log(answer.reduce((r,v)=>{return r+v},0))
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함