Basic Calculator
Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation. The expression string may contain open ( and closing ) parentheses, the plus + or minus - sign, non-negative integers and empty spaces.

30:00

Basic Calculator
hard
Topics
Companies

Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation. The expression string may contain open ( and closing ) parentheses, the plus + or minus - sign, non-negative integers and empty spaces.

Example 1:
Input: "(1+(4+5+2)-3)+(6+8)"
Output: 23
Constraints:
  • 1s.length3×1051 \leq s.\text{length} \leq 3 \times 10^5

  • s consists of digits, '+', '-', '(', ')', and ' '.

  • s represents a valid expression.

  • All intermediate results fit in a signed 32-bit integer.

Input
arr ="(1+(4+5+2)-3)+(6+8)"
Expression

(

1

+

(

4

+

5

+

2

)

-

3

)

+

(

6

+

8

)

num
0
sign
+
result
0
Stack (result, sign pairs)

Empty

Initialize: result=0, sign=1

Variables
VariableValue
result0
sign1
DepthFunction Call
Stack empty
0/20