Your function should return the integer result of evaluating the expression. Division should be integer division (truncating toward zero). The expression is guaranteed to be valid and properly formatted.
Requirements
Correctly handle operator precedence: multiplication and division have higher precedence than addition and subtraction
Evaluate expressions within parentheses before applying operators outside them
Support unary minus operators at the beginning of the expression or after an opening parenthesis
Handle multi-digit numbers correctly
Ignore whitespace characters in the input
Perform integer division, truncating the result toward zero
Constraints
1 ≤ s.length ≤ 10^4
s consists of digits, '+', '-', '*', '/', '(', ')', and spaces
s represents a valid expression
All intermediate calculations will fit in a 32-bit integer
Division by zero will not occur in the input
Examples
Example 1:
Input: s = "1 + 1" Output: 2 Explanation: Simple addition with spaces
Example 2:
Input: s = "6-4/2" Output: 4 Explanation: Division has higher precedence, so 4/2 = 2, then 6-2 = 4
Example 3:
Input: s = "2*(5+5*2)/3+(6/2+8)" Output: 21 Explanation: Evaluates innermost parentheses first, then works outward respecting precedence
Example 4:
Input: s = "-(3+4)" Output: -7 Explanation: Unary minus applied to the result of the parenthesized expression