Back to DSA

Basic Calculator

hard
Acceptance: 38%
StackMathString

Write a calculator that evaluates a string expression containing non-negative integers, the operators + and -, parentheses for grouping, and optional spaces. The expression is guaranteed to be well-formed. Return the computed integer result.

Examples

Example 1:
Input:s = "5 - 3 + 2"
Output:4
Example 2:
Input:s = "(2+(3+4+1)-2)+(7+9)"
Output:24

Hints

00:00
1234567