Evaluate a Postfix Arithmetic Expression

Implement evaluatePostfix

Given a postfix arithmetic expression expr containing single-digit operands and the operators +, -, *, /, evaluate it and return the result. Postfix notation is built specifically to make evaluation trivial with a stack: since every operator immediately follows its two already-resolved operands, there's never any need to look ahead or worry about precedence — push operands as they're read, and whenever an operator is seen, pop the two most recent values, apply it, and push the result back. By the time the whole expression has been scanned, exactly one value remains on the stack: the answer.

Example 1:

Input: expr = "53+82-*"

Output: 48

Example 2:

Input: expr = "462/*"

Output: 12

Example 3:

Input: expr = "84*7-"

Output: 25

+ 5 hidden test cases run on Submit.

Constraints:

  • 1 ≤ expr.length ≤ 9
  • expr contains only single-digit operands (0-9) and the operators '+', '-', '*', '/'
  • Division truncates toward zero
  • expr is guaranteed to be a valid postfix expression

expr =

53+82-*