(자바) 백준 10808 스택

문제가 있는 링크

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

내 녹음

알고리즘 분류: 스택

성공:

문제 난이도: 실버 4

난이도 : 매우 쉬움

접근하다

스택에서 각 기능을 경험하는 것은 느낌의 문제입니다.

스택 연습에 좋습니다.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class bj10828_스택 {
    public static void main(String() args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());

        Stack<Integer> stack = new Stack<>();
        for(int i = 0 ; i < N ; i++){
            String() input = br.readLine().split(" ");

            if(input(0).equals("push")){
                stack.push(Integer.parseInt(input(1)));
            } else if(input(0).equals("pop")){
                System.out.println(stack.isEmpty() ? -1 : stack.pop());
            } else if(input(0).equals("size")){
                System.out.println(stack.size());
            } else if(input(0).equals("empty")){
                System.out.println(stack.isEmpty() ? 1 : 0);
            } else if(input(0).equals("top")){
                System.out.println(stack.isEmpty() ? -1 : stack.peek());
            }
        }
    }
}