Skip to content
BASA DSA

Evaluate Reverse Polish Notation

MediumStack

Evaluate integer tokens in postfix notation using +, -, * and /. Division truncates toward zero. Return the final integer.

Example 1

Input
tokens = ["5","2","-","4","*"]
Output
12

Explanation

Compute 5-2=3, then multiply by 4 to obtain 12.

Example 2

Input
tokens = ["-7","3","/"]
Output
-2

Explanation

-7/3 truncates toward zero to -2 rather than flooring to -3.

Example 3

Input
tokens = ["2","3","4","*","+"]
Output
14

Explanation

Complete 3*4 before adding 2, producing 14.

Constraints

  • 1 <= number of tokens <= 10000
  • Expression is valid
  • Divisors are nonzero
  • Intermediate values fit signed 32-bit integers

Function contractevalRPNArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…