-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMain.java
55 lines (49 loc) · 1.55 KB
/
Main.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
int N = sc.nextInt();
int size = 0;
int[] stack = new int[N];
for(int i = 0; i<N; i++){
switch(sc.next()){
case "push":
int n = Integer.parseInt(sc.next());
stack[size] = n;
size++;
break;
case "pop":
if (size>0){
sb.append(stack[size-1]).append('\n');
stack[size-1] = 0;
size--;
}else{
sb.append(-1).append('\n');
}
break;
case "size":
sb.append(size).append('\n');
break;
case "empty":
if(size == 0){
sb.append(1).append('\n');
}else{
sb.append(0).append('\n');
}
break;
case "top":
if(size == 0){
sb.append(-1).append('\n');
}else{
sb.append(stack[size-1]).append('\n');
}
break;
default:
break;
}
}
System.out.println(sb);
sc.close();
}
}