import java.util.*;
public class Main {
static List<Integer> stk = new ArrayList<Integer>();
static int index = -1;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuffer sb = new StringBuffer();
int N = sc.nextInt();
for(int i = 1; i <= N; i++) {
String cmd = sc.next();
if("push".equals(cmd)) {
push(sc.nextInt());
} else if("pop".equals(cmd)) {
sb.append(pop()).append("\n");
} else if("size".equals(cmd)) {
sb.append(size()).append("\n");
} else if("empty".equals(cmd)) {
sb.append(empty()).append("\n");
} else if("top".equals(cmd)) {
sb.append(top()).append("\n");
}
}
System.out.println(sb.toString());
}
public static void push(int param) {
stk.add(param);
index++;
}
public static int pop() {
if(empty() == 1) {
return index;
}else {
int popItem = stk.get(index);
stk.remove(index);
index--;
return popItem;
}
}
public static int size() {
return stk.size();
}
public static int empty() {
if(stk.isEmpty()) {
return 1;
}else {
return 0;
}
}
public static int top() {
if(empty() == 1) {
return index;
}else {
return stk.get(index);
}
}
}
StringBuffer로 해야 시간제한 통과
'JAVA > 백준' 카테고리의 다른 글
[스택] 백준 9012 (0) | 2021.08.03 |
---|---|
[문자열 처리] 백준 11656 (0) | 2021.07.31 |
[문자열 처리] 백준 10824 (0) | 2021.07.31 |
[문자열 처리] 백준 11655 (0) | 2021.07.30 |
[문자열 처리] 백준 10820 (0) | 2021.07.30 |