Skip to contentKEEP THE CRAFTTHE SANCTUARY

Evaluate Reverse Polish Notation

Evaluate an arithmetic expression written in postfix order with a stack.

The problem

Implement Solution.evalRPN(tokens). tokens is an arithmetic expression in Reverse Polish Notation: each operator comes after its two operands. Every token is either an integer written as a string, such as "-3", or one of "+", "-", "*" and "/". Return the value of the expression as an integer. Division between two integers truncates toward zero, so 8 / -3 is -2. For example, ["2", "1", "+", "3", "*"] means (2 + 1) * 3, which is 9. The expression is always valid, never divides by zero, and every intermediate value fits in a 32-bit signed integer.

Starter code

class Solution:
    def evalRPN(self, tokens):
        pass

Public examples

  1. tokens = ["2", "1", "+", "3", "*"]gives9
  2. tokens = ["5", "2", "-"]gives3
  3. tokens = ["8", "-3", "/"]gives-2

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.