Basic Calculator

Hard

Evaluate a valid expression containing non-negative integers, +, -, (, ) and spaces, and return its integer value.

You must not use eval.

Example 1

calculate()
Input
s = "1 + 1"
Output
2

Example 2

calculate()
Input
s = " 2-1 + 2 "
Output
3
Constraints
1 <= s.length <= 3 * 10^5
The expression is always valid.

Topics

StringsStackMath

Implement calculate in the editor and return the answer — printing it is never graded.