← 返回 instacart 的题目列表Evaluate an Arithmetic Expression
类型:online_judge
Given a string expr representing an arithmetic expression, evaluate it.
Rules
expr contains non-negative integers, operators + - * /, parentheses ( ), and spaces.
Operator precedence: * and / have higher precedence than + and -.
Parentheses may override precedence.
Division is integer division truncated toward zero (e.g., 7/3 = 2, -7/3 = -2).
Return the evaluated result (fits in 32-bit signed integer).
Examples
Input: "3+2*2" Output: 7
Input: " (1+(4+5+2)-3) + (6+8) " Output: 23
Constraints (typical interview setting)
1 <= len(expr) <= 1e5
Test cases
Example
Input
3+2*2
Output
7