Basic Calculator II implements LeetCode problem 227, a common Meta/Facebook interview question tagged with String, Stack, and Math. It requires evaluating arithmetic expressions with +, -, *, / while respecting operator precedence.
Given a string s representing a valid arithmetic expression with non-negative integers and operators +, -, *, / (spaces allowed), compute and return the integer result. Division truncates toward zero (e.g., -8/2 = -4). No built-in eval() allowed.[1][3]
| Input | Output | Explanation |
|-------|--------|-------------|
| "3+2*2" | 7 | 3 + (2*2) = 3 + 4 = 7 [1] |
| " 3/2 " | 1 | 3/2 = 1 (truncates toward zero) [1] |
| " 3+5 / 2 " | 5 | 3 + (5/2) = 3 + 2 = 5 [1] |
| "8*5/4+3-2" | 11 | (8*5)/4 + 3 - 2 = 40/4 + 3 - 2 = 10 + 3 - 2 = 11 [7] |
1 <= s.length <= 3 * 10^5s consists of digits, spaces, and operators +, -, *, /[0, 2^31 - 1]