279. Basic Calculator

HardStringString

Given a string s representing a valid arithmetic expression with non-negative integers, '+', '-', parentheses, and spaces, evaluate it and return the result. There is no multiplication or division. Unary minus may appear after '(' .

Input: A quoted expression string.

Output: Integer — the evaluated result.

Examples

Example 1
Input: "1 + 1"
Output: 2
Explanation: Simple addition.
Example 2
Input: "(1+(4+5+2)-3)+(6+8)"
Output: 23
Explanation: Nested parentheses.
Example 3
Input: " 2-1 + 2 "
Output: 3
Explanation: Spaces ignored.

Constraints

Asked by

AmazonMicrosoftGoogleMetaBloombergAdobe
Solve this problem in the editor →