-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjava10828.java
37 lines (33 loc) · 1.04 KB
/
java10828.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.*;
public class java10828 {
public static Stack<Integer> stack = new Stack<>();
public static StringBuilder sb = new StringBuilder();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
String st = sc.next();
if(st.equals("push")){
int a = sc.nextInt();
stack.push(a);
}
else if(st.equals("top"))
{
sb.append(stack.isEmpty() ? -1 : stack.peek()).append('\n');
}
else if(st.equals("size"))
{
sb.append(stack.size()).append('\n');
}
else if(st.equals("empty"))
{
sb.append(stack.isEmpty() ? 1 : 0).append('\n');
}
else if(st.equals("pop"))
{
sb.append(stack.isEmpty() ? -1 : stack.pop()).append('\n');
}
}
System.out.println(sb);
}
}