Skip to content

Commit ff59c63

Browse files
authored
Update Baseball Game.java
1 parent 4b69e86 commit ff59c63

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

Easy/Baseball Game.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
class Solution {
22
public int calPoints(String[] ops) {
33
Stack<Integer> stack = new Stack<>();
4-
int sum = 0;
4+
int totalScore = 0;
55
for (String op : ops) {
6-
if (op.equals("+")) {
7-
int prev = stack.pop();
8-
int prevToPrev = stack.peek();
9-
int newScore = prev + prevToPrev;
10-
sum += newScore;
11-
stack.push(prev);
6+
if (op.equals("D")) {
7+
int newScore = 2 * stack.peek();
8+
totalScore += newScore;
129
stack.push(newScore);
13-
}
14-
else if (op.equals("C")) {
15-
sum -= stack.pop();
16-
}
17-
else if (op.equals("D")) {
18-
sum += stack.peek() * 2;
19-
stack.push(stack.peek() * 2);
20-
}
21-
else {
10+
} else if (op.equals("C")) {
11+
totalScore -= stack.pop();
12+
} else if (op.equals("+")) {
13+
int scoreTwo = stack.pop();
14+
int newScore = scoreTwo + stack.peek();
15+
stack.push(scoreTwo);
16+
stack.push(newScore);
17+
totalScore += newScore;
18+
} else {
2219
stack.push(Integer.parseInt(op));
23-
sum += stack.peek();
20+
totalScore += Integer.parseInt(op);
2421
}
2522
}
26-
return sum;
23+
return totalScore;
2724
}
2825
}

0 commit comments

Comments
 (0)