반응형

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		Stack<Integer> stack = new Stack<>();
		StringBuilder sb = new StringBuilder();
		
		int n = sc.nextInt();
		for(int i=0;i<n;i++) {
			String command = sc.next();
			
			if(command.equals("push")) {
				int value = sc.nextInt();
				stack.push(value);
			} else if(command.equals("pop")){
				if(stack.empty()) {
					sb.append(-1 +"\n");
				} else {
					sb.append(stack.pop()+"\n");
				}
			} else if(command.equals("size")) {
				sb.append(stack.size()+"\n");
			} else if(command.equals("empty")) {
				if(stack.empty()) {
					sb.append(1 +"\n");
				} else {
					sb.append(0 +"\n");
				}
			} else if(command.equals("top")) {
				if(stack.empty()) {
					sb.append(-1 +"\n");
				} else {
					sb.append(stack.peek()+"\n");
				}
			}
		}
		
		System.out.println(sb);
	}

}
반응형

'알고리즘 > Baekjoon' 카테고리의 다른 글

[Baekjoon] #11651 좌표 정렬하기2  (0) 2022.06.28
[Baekjoon] #10815 숫자 카드  (0) 2022.06.22
[Baekjoon] #10989 수 정렬하기3  (0) 2022.06.22
[Baekjoon] #11047 동전 0  (0) 2022.06.22
[Baekjoon] #11050 이항 계수 1  (0) 2022.06.22

+ Recent posts