-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackOp.java
More file actions
36 lines (30 loc) · 825 Bytes
/
StackOp.java
File metadata and controls
36 lines (30 loc) · 825 Bytes
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
import java.util.Stack;
public class StackOp {
public static int BaseBallSum(String [] ops){
Stack<Integer> st = new Stack<>();
for (String op : ops) {
if (op.equals("+")) {
int top = st.pop();
int newScore = top + st.peek();
st.push(top);
st.push(newScore);
}
else if (op.equals("C")) {
st.pop();
}
else if (op.equals("D")) {
st.push(st.peek() * 2);
}
else {
st.push(Integer.parseInt(op));
}
}
int totSum = 0;
while (!st.isEmpty()) {
totSum += st.pop();
}
return totSum;
}
public static void main(String[] args) {
}
}