forked from JustinLeeRepo-zz/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SuperStack.java
50 lines (43 loc) · 1.28 KB
/
SuperStack.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
public class SuperStack{
private static ArrayList<Integer> stack = new ArrayList<Integer>();
public static String push(int val){
stack.add(val);
return String.valueOf(stack.get(stack.size() - 1));
}
public static int pop(){
if(stack.size() > 0){
stack.remove(stack.size() - 1);
}
if(stack.size() > 0)
return String.valueOf(stack.get(stack.size() - 1));
return "EMPTY";t
}
public static void inc(int numElem, int incVal){
for(int i = 0; i < numElem; i++){
stack.set(i, stack.get(i) + incVal);
}
return String.valueOf(stack.get(stack.size() - 1));
}
public static String validate(String[] str){
switch(str[0]){
case "push":
if(str.length != 2){
return "ERROR: PUSH CALL USAGE";
}
return push(Integer.valueOf(str[1]));
}
case "pop":
if(str.length != 1){
return "ERROR: POP CALL USAGE";
}
return pop();
case "inc":
if(str.length != 3){
return "ERROR: INC CALL USAGE";
}
return inc(Integer.valueOf(str[1]), Integer.valueOf(str[2]));
}
}
public static void main(String[] args){
}
}