2025-07-31 07:28:31 +0000 UTC
Baseball Game
Categories:
Links
Code
class Solution:
def calPoints(self, operations: List[str]) -> int:
stack = []
stack_sum = 0
for op in operations:
if op.isnumeric() or op.startswith("-"):
new_score = int(op)
stack.append(new_score)
stack_sum += new_score
elif op == "+":
new_score = stack[-1] + stack[-2]
stack_sum += new_score
stack.append(new_score)
elif op == "D":
new_score = stack[-1] * 2
stack_sum += new_score
stack.append(new_score)
elif op == "C":
stack_sum -= stack[-1]
stack.pop()
else:
raise Exception(op)
return stack_sum