-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0682-baseball-game.java
More file actions
35 lines (30 loc) · 875 Bytes
/
0682-baseball-game.java
File metadata and controls
35 lines (30 loc) · 875 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
class Solution {
public int calPoints(String[] ops) {
Stack<Integer> stack = new Stack<Integer>();
int res = 0;
for (String s: ops) {
if (s.equals("C")) {
stack.pop();
}
else if (s.equals("D")) {
int prev = stack.peek();
stack.push(prev*2);
}
else if (s.equals("+")) {
int n1 = stack.pop();
int n2 = stack.peek();
int n3 = n1 + n2;
stack.push(n1);
stack.push(n3);
}
else {
int n = Integer.parseInt(s);
stack.push(n);
}
}
while (!stack.isEmpty()) {
res += stack.pop();
}
return res;
}
}