98. Evaluate Reverse Polish Notation

Medium · Stack

Evaluate an expression written in Reverse Polish Notation (RPN). You are given an array of tokens representing numbers (as strings) and operators (+, −, ×, ÷). Process them using a stack: push numbers onto the stack, and when you encounter an operator, pop the required operands, apply the operation, and push the result back. Return the final integer result. Note: division truncates toward zero (so 13 ÷ 5 = 2, and −13 ÷ 5 = −2).

Examples

Example 1
Input: ["2","1","+","3","*"]
Output: 9
Explanation: (2+1) * 3 = 9
Example 2
Input: ["4","13","5","/","+"]
Output: 6
Explanation: 4 + (13/5) = 4 + 2 = 6

Constraints