-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseball-Game.java
More file actions
34 lines (32 loc) · 986 Bytes
/
Copy pathBaseball-Game.java
File metadata and controls
34 lines (32 loc) · 986 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
class Solution {
public int calPoints(String[] ops) {
Stack<Integer> stack = new Stack<>();
for (String op : ops) {
switch (op) {
case "C":
// Remove the previous score
stack.pop();
break;
case "D":
// Double the previous score
stack.push(stack.peek() * 2);
break;
case "+":
// Sum of the last two scores
int top = stack.pop();
int newTop = top + stack.peek();
stack.push(top);
stack.push(newTop);
break;
default:
// It's a number
stack.push(Integer.parseInt(op));
}
}
int sum = 0;
for (int score : stack) {
sum += score;
}
return sum;
}
}