-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseballgame.java
More file actions
38 lines (36 loc) · 943 Bytes
/
baseballgame.java
File metadata and controls
38 lines (36 loc) · 943 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
37
38
import java.util.Stack;
public class baseballgame {
public static int calPoints(String[] operations) {
Stack<Integer> s=new Stack<>();
int score=0;
for (String ops : operations) {
if (ops.equals("+")) {
int meh=s.get(s.size()-1)+s.get(s.size()-2);
s.push(meh);
}
else if (ops.equals("D")) {
s.push(2*s.peek());
}
else if (ops.equals("C")) {
s.pop();
}
else {
s.push(Integer.valueOf(ops));
}
}
for (Integer i : s) {
score+=i;
}
return score;
}
}
/*
An integer x.
Record a new score of x.
'+'.
Record a new score that is the sum of the previous two scores.
'D'.
Record a new score that is the double of the previous score.
'C'.
Invalidate the previous score, removing it from the record.
*/