Plus Minus With Parentheses
expr is an arithmetic expression made of non-negative integers, the operators + and -, round brackets and spaces. Brackets may be nested, and a - may stand directly before a bracket, as in 3 - (1 + 2). The expression is valid: it and the inside of every bracket start with a number or an opening bracket, so there are no unary signs. Operators of the same level are applied from left to right.
Return the value of the expression as an integer. It may be negative.
Example 1
expr "3 - (1 + 2)"
returns 0
The bracket is worth 3, and 3 - 3 is 0.
Example 2
expr "(1+2)-(3-(4-5))"
returns -1
(4-5) is -1, so (3-(4-5)) is 4, and 3 - 4 is -1.
Constraints
1 <= len(expr) <= 60- every number is between
0and999and has no leading zeros (except the number0itself) - brackets are nested at most 5 deep
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Stacks and queues problems
- Remove Adjacent Duplicates in Groups of KMedium
- Remove K DigitsMedium
- Rotating Queue With CooldownMedium
- Round Robin Completion TimesMedium
- Score of ParenthesesMedium
- Self Time per FunctionMedium
- Simplify PathMedium
- Two Teams in a CircleMedium