문제
BOJ 10828 스택
접근방법
- 스택 기본 문제
- 스택에 대해 알고있고, java의 stack의 메서드들 활용하면 쉽게 풀이
구현
import java.io.*;
import java.util.*;
class Main {
public static Stack<Integer> stack = new Stack<>();
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine(), " ", false);
String cmd = st.nextToken();
if (cmd.equals("push")) {
int token = Integer.parseInt(st.nextToken());
stack.push(token);
} else if (cmd.equals("top")) {
if (stack.size() == 0)
bw.write("-1\n");
else
bw.write(stack.peek() + "\n");
} else if (cmd.equals("size")) {
bw.write(stack.size() + "\n");
} else if (cmd.equals("pop")) {
if (stack.size() == 0)
bw.write("-1\n");
else
bw.write(stack.pop() + "\n");
} else if (cmd.equals("empty")) {
bw.write(stack.empty() ? "1" : "0");
bw.write("\n");
}
}
bw.close();
}
}
제출