In Reverse Polish Notation the operator comes after the two numbers it works on. So 3 + 4 is written as 3 4 +. There are no brackets.
The array tokens holds such an expression. The operators are +, -, * and /. Evaluate the expression and return the result.
When you divide two integers, cut off the decimal part (round toward zero). So -9 / 4 is -2. The expression is always valid, and there is no division by zero.
Examples
Example 1:
Input: tokens = ["3","4","+","2","*"]
Output: 14
Explanation: (3 + 4) * 2 = 14.
Example 2:
Input: tokens = ["-9","4","/"]
Output: -2
Explanation: -9 / 4 is -2.25, and rounding toward zero gives -2.
Example 3:
Input: tokens = ["8","3","-","2","6","*","+"]
Output: 17
Explanation: (8 - 3) + (2 * 6) = 5 + 12 = 17.