772. Basic Calculator III
Implement a calculator that evaluates a valid arithmetic expression containing:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Integer division (/)
- Parentheses for grouping
- Spaces, which may be ignored
The expression contains non-negative integers and is always valid. Division is integer division that truncates toward zero.
Examples
Input: "1+1*1"
Output: 2
Input: "(2+4)*6"
Output: 36
Hints
Use two stacks: one for numbers, one for operators.
Handle operator precedence: * and / before + and -.
Process parentheses by evaluating the inner expression recursively or using a new scope.
Related Problems
772. Basic Calculator III
Implement a calculator that evaluates a valid arithmetic expression containing: