알고리즘 문제풀이
[백준 - 10828 Java] 스택
TutleKing
2023. 3. 14. 21:51
기본적인 스택 문제였다. 그냥 스택을 쓰면 되는 문제였겠지..? 배열을 썼어야한건가... 일단 통과
import java.util.*;
import java.io.*;
public class Main {
static Stack<Integer> stack = new Stack<>();
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int count = Integer.parseInt(br.readLine());
for (int i = 0; i < count; i++) {
int num = myStack(br.readLine());
if (num == Integer.MAX_VALUE) {
continue;
}
System.out.println(num);
}
br.close();
}
static int myStack(String cmd) {
String[] split = cmd.split(" ");
int num = -1;
if (split.length == 2) {
num = Integer.parseInt(split[1]);
}
if (split[0].equals("push")) {
stack.push(num);
return Integer.MAX_VALUE;
} else if (split[0].equals("top")) {
if (stack.isEmpty()) {
return -1;
}
return stack.peek();
} else if (split[0].equals("size")) {
return stack.size();
} else if (split[0].equals("empty")) {
return stack.isEmpty() ? 1 : 0;
} else if (split[0].equals("pop")) {
if (stack.isEmpty()) {
return -1;
}
return stack.pop();
}
return -1;
}
}
반응형