Tech for good

[Leetcode/Stack] 682. Baseball Game 본문

IT/Computer Science

[Leetcode/Stack] 682. Baseball Game

Diana Kang 2025. 4. 16. 22:16

🌷 Stack

  • push(): 데이터를 넣는다/추가한다 (+) / Adds x to the top of the stack
  • pop(): 데이터를 꺼낸다/뺀다 (-) / Removes and returns the top item
  • append(): Add an element x to the end of a list

class Solution:
    def calPoints(self, operations: List[str]) -> int:
        stack = []

        for op in operations:
            if op == "C":
                stack.pop()
            elif op == "D":
                stack.append(2 * stack[-1])
            elif op == "+":
                stack.append(stack[-1] + stack[-2])
            else:
                stack.append(int(op))
        return sum(stack)