Basic Calculator
Implement a basic calculator that evaluates a string expression containing non-negative integers, '+', '-', '(', ')' and spaces. There is no multiplication or division. Return the integer result. You must handle nested parentheses and unary-like leading signs correctly, without using a built-in eval.
Open official problem prompt ↗Evaluate an expression with plus, minus, and arbitrarily nested parentheses to a single integer.
Doing running arithmetic on paper, but each time you open a bracket you jot down the total and the +/- sitting in front of it on a shelf, then start fresh; closing the bracket takes them back off the shelf.
- Input
- s = "1-(2+3)"
- Output
- -4
- Why
- The parentheses evaluate to 2+3 = 5, and 1 - 5 = -4.
1 <= s.length <= 3 * 10^5s consists of digits, '+', '-', '(', ')' and ' 's is a valid expressionThe final result and all intermediate values fit in a 32-bit signed integer