← 返回 google 的题目列表Evaluate a Nested Math Expression String (add/sub function calls)
类型:online_judge
You are given a string s representing a math expression. The expression is written using nested function calls:
add(x, y) means x + y
sub(x, y) means x - y
Both x and y can be integers or another nested function-call expression.
Compute and return the integer result of the expression.
Input
A single line string s.
Output
Print one integer: the evaluated result.
Constraints
1 <= len(s) <= 2 * 10^5
Numbers are base-10 integers within 32-bit signed range
The string contains only lowercase letters, digits, (, ), , (unless specified otherwise)
The expression is guaranteed to be valid (balanced parentheses, correct commas/arity)
Aim for linear (or near-linear) time complexity
Example
Input: add(1, sub(1, 0)) Output: 2
Tests
Input: add(1,sub(1,0)) Output: 2
Input: sub(10,3) Output: 7
Input: add(sub(5,2),sub(1,4)) Output: 0
Input: add(0,add(0,0)) Output: 0
Input: sub(add(7,8),sub(3,1)) Output: 13
Example
Input
add(1,sub(1,0))
Output
2