expr is a valid arithmetic expression made of non-negative integers, the signs + and -, round brackets and spaces. Evaluate it and return the result.
A minus may be unary: -7 and -(1 + 2) are allowed. A plus is never unary, and two signs never stand next to each other. Spaces may appear anywhere between numbers and signs.
Do not use anything built in that evaluates a string as an expression (such as eval): parse the string yourself.expr="12 - (4 + 3) + 1" → 6
The bracket gives 7, then 12 - 7 + 1 = 6.
expr="-(8 - (2 + 9))" → 3
Inside it is 8 - 11 = -3, and the minus in front of the bracket flips it to 3.
expr=" 40 " → 40
expr consists of digits, +, -, (, ) and spacesexpr is a valid expressionexpr="12 - (4 + 3) + 1"
6
Run checks the first 3 tests in your browser. Submit checks all 80 tests on the server, hidden ones included.