1006. Evaluate a Postfix (RPN) Expression
Medium · Stack
Evaluate a postfix expression with integer operands and the four basic operators (+, -, *, /). Use a stack.
Tokens are space-separated. For division, use truncated integer division (Python //), and assume no division by zero in test cases.
Input (stdin, JSON): { "expr": "3 4 + 5 *" }
Output (stdout): the evaluated integer result.
Example: "3 4 +" → 7; "3 4 + 5 *" → 35; "1 2 3 * +" → 7.
Examples
Example 1
Input: {"expr":"3 4 +"}
Output: 7Example 2
Input: {"expr":"3 4 + 5 *"}
Output: 35Constraints
- Tokens space-separated; valid RPN.
- Operands fit in 64-bit signed int.