Basic Calculator
HardStack
Decode it first. Clues, constraints, pattern and plan before any code.Given a string s holding a valid expression, return its value. The expression contains non-negative integers, +, -, parentheses ( and ), and spaces. A - can also be unary (it negates what follows, as in -(2+3) or -5). There is no * or /. Do not use any built-in function that evaluates strings, like eval.
s = "1 + 1" → 2
s = " 2-1 + 2 " → 3
s = "(1+(4+5+2)-3)+(6+8)" → 23
s = "-(2+3)" → -5Constraints
1 <= s.length <= 3 * 10^5scontains digits,+,-,(,)and spaces.sis a valid expression.+is never unary;-may be.- Every number and every intermediate result fits in a 32-bit integer.