Dodomeki
Sign in

60. Postfix Evaluation

Medium

Evaluate an expression in postfix notation given in tokens. The tokens are integers or the binary operators +, -, *, and /. Division truncates toward zero. The input always forms a valid expression and does not divide by zero.

Example 1
Input: tokens = ["7", "2", "-", "3", "*"]
Output: 15
Explanation: First 7 - 2 = 5; then 5 * 3 = 15.
Example 2
Input: tokens = ["8", "3", "/"]
Output: 2
Explanation: The division 8 / 3 is truncated to 2.
Constraints
  • 1 <= tokens.length <= 10^4
  • Cada resultado intermediário cabe em inteiro de 32 bits.
StackMath
Caso 1
tokens =
["7","2","-","3","*"]
Expected =
15
Caso 2
tokens =
["8","3","/"]
Expected =
2

Run checks these 2 cases. Submit runs all 6, including hidden ones.