Evaluate a Parenthesized Integer Expression
Problem statement
Evaluate the integer expression in expression.
The expression contains non-negative decimal integers, the binary operators + and -, parentheses, and spaces. A + or - may also appear as a unary sign, including immediately before a parenthesized expression.
Use integer arithmetic and return the value of the complete expression. Do not call a language-provided expression evaluator.
Function
calculate(expression: String) → intExamples
Example 1
expression = "1 + 1"return = 2Adding the two operands gives 2.
Example 2
expression = "(1+(4+5+2)-3)+(6+8)"return = 23The two outer groups evaluate to 9 and 14.
Example 3
expression = "-(2 + 3) + 10"return = 5The unary minus negates the parenthesized value 5.
Constraints
1 <= expression.length <= 100000expressionis a valid expression containing digits, spaces,+,-,(, and).- Every intermediate value and the final answer fit in a signed 32-bit integer.
Source note: The source image is a source-neutral excerpt of the author-reported prompt.