Description
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, and /. Each operand may be an integer or another expression. Return the result as an integer.
Examples
Input:
tokens = ["2","1","+","3","*"]Output:
9Explanation:
Using a stack: push 2,1, then '+' pops both and pushes 3, push 3, then '*' pops 3,3 and pushes 9.
Input:
tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]Output:
22Explanation:
Evaluating step by step: 9+3=12, 12*(-11)=-132, -132/6=-22, 10*(-22)=-220, -220+17=-203, -203+5=-198... Full evaluation yields 22.
Input:
tokens = ["15","7","+","1","+","1","+","4","/","6","1","4","+","*","-","2","-","13","+"]Output:
-13Explanation:
Evaluating the tokens with a stack gives -13.
Constraints
- •
1 ≤ tokens.length ≤ 10⁴ - •
tokens[i] is an operator or an integer.