Skip to content

Practice Baseball Game

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#682
LeetCode Problem

Baseball Game

Problem Statement

Start with an empty score record and process operations in order. An integer appends that score; + appends the sum of the previous two scores; D appends twice the previous score; C removes the previous score. Return the sum of the final record. Every operation is valid, and all intermediate calculations and the result fit signed 32-bit integers.

Source: Baseball Game on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
ops = ["5","2","C","D","+"]
Output
30

# Example 2

Input
ops = ["5","-2","4","C","D","9","+","+"]
Output
27

# Example 3

Input
ops = ["1","C"]
Output
0

Constraints

1 <= operations.length <= 1000
operations[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 10^4, 3 * 10^4].
For operation "+", there will always be at least two previous scores on the record.
For operations "C" and "D", there will always be at least one previous score on the record.

Related LeetCode Problems

Practice more problems similar to Baseball Game.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.