Hard · Stacks & Queues

Basic Calculator

Given a string s holding a valid expression of non-negative integers, '+', '-', parentheses and spaces, where '-' may also be unary ("-2", "-(2 + 3)"), return the value of the expression without using a built-in evaluator.

Examples

Example 1

{
  "s": "(1+(4+5+2)-3)"
}

Output: = 9

Example 2

{
  "s": "1-(2+3)"
}

Output: = -4

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Stacks & Queues problems