578. Baseball Game Score

EasyStackStack

You keep a running record of scores. Given a list of string operations, apply each: an integer x records a new score x; '+' records the sum of the previous two scores; 'D' records double the previous score; 'C' removes the previous score. Return the total sum of all recorded scores after processing every operation. The input is JSON {ops}.

Input: JSON {ops} — a list of string operations.

Output: Integer — the sum of all recorded scores.

Examples

Example 1
Input: {"ops":["5","2","C","D","+"]}
Output: 30
Explanation: Records 5, then 5+10+15.
Example 2
Input: {"ops":["1"]}
Output: 1
Explanation: A single score.
Example 3
Input: {"ops":["5","-2","4","C","D","9","+","+"]}
Output: 27
Explanation: Applies each operation in order.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →