-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseBall.java
More file actions
30 lines (26 loc) · 761 Bytes
/
BaseBall.java
File metadata and controls
30 lines (26 loc) · 761 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
// 682.Baseball Game
import java.util.Stack;
class Solution {
public int calPoints(String[] operations) {
Stack<Integer> stack = new Stack<>();
for (String op : operations) {
if (op.equals("C")) {
stack.pop();
} else if (op.equals("D")) {
stack.push(stack.peek() * 2);
} else if (op.equals("+")) {
int top = stack.pop();
int newTop = top + stack.peek();
stack.push(top);
stack.push(newTop);
} else {
stack.push(Integer.parseInt(op));
}
}
int sum = 0;
for (int score : stack) {
sum += score;
}
return sum;
}
}