Asked at

Baseball Game

Easy
Verified
Stack~12 min

Given ops, apply each operation to a running record of scores: an integer string records that score, "+" records the sum of the last two scores, "D" records double the last score, and "C" removes the last score. Return the total of all recorded scores.

The input arrives as a single array ops. Use a stack of recorded scores.

Examples

in["5", "2", "C", "D", "+"]
out30

Records 5, 2; cancel 2; double 5 -> 10; sum 5+10 -> 15. Total 5+10+15.

in["1", "C"]
out0

Record 1, then cancel it.

Constraints

  • 1 ≤ ops.length ≤ 1000
  • Each op is an integer string, "+", "D", or "C".
  • Operations needing prior scores always have them.

Get help

🔑

Sign in to solve

Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.