Skip to content

Files

Latest commit

9e1b505 · Sep 4, 2022

History

History

150-EvaluateReversePolishNotation

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Sep 4, 2022
May 25, 2022

Evaluate Reverse Polish Notation

Problem can be found in here!

def evalRPN(tokens: List[str]) -> int:
    stack = []
    for token in tokens:
        if token in set("+-*/"):
            right_operand, left_operand = int(stack.pop()), int(stack.pop())
            if token == "+":
                result = (left_operand + right_operand)
            elif token == "-":
                result = (left_operand - right_operand)
            elif token == "*":
                result = (left_operand * right_operand)
            else:
                result = int(left_operand / right_operand)
            stack.append(result)
        else:
            stack.append(token)

    return stack[-1]

Time Complexity: O(n), Space Complexity: O(n)