Skip to content

Latest commit

 

History

History

150-EvaluateReversePolishNotation

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

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)