Given a valid infix arithmetic expression containing non-negative integers, parentheses ( ), and the binary operators +, -, *, and /, evaluate the expression and return its result as double data type. Division should truncate toward zero.
Implement the Shunting-Yard algorithm to first convert the infix expression to Reverse Polish Notation (postfix), then evaluate the postfix expression.
A single line containing the infix expression string s.
s consists of digits (0–9), spaces, parentheses ( and ), and operators +, -, *, /.Double (data type) representing the evaluated result of the expression.
1 <= s.length <= 10^5| Example | Input | Output | Explanation |
|---|---|---|---|
| 1 | 3 + 2 * 2 | 7 | 2*2 = 4, then 3+4 = 7 |
| 2 | 3/2 | 1 | 3/2 truncated toward zero yields 1 |
| 3 | (1+(4+5+2)-3)+(6+8) | 23 | Evaluate inner parentheses step by step |
https://drawtocode.vercel.app/problems/evaluate-infix-expression
Loading component...
Loading component...