Basic Calculator II
Evaluate a string arithmetic expression containing non-negative integers and the operators '+', '-', '*', '/' separated by optional spaces, with no parentheses. Multiplication and division have higher precedence than addition and subtraction, and integer division truncates toward zero. Return the integer result.
Open official problem prompt ↗Evaluate a parenthesis-free expression while respecting that '*' and '/' bind tighter than '+' and '-'.
Reading a bill left to right: additions and subtractions go into a running list of line items, but a 'times' or 'divided by' immediately rewrites the last line item before you total everything.
- Input
- s = "3+2*2"
- Output
- 7
- Why
- '*' binds tighter than '+', so 2*2 = 4 is computed first, then 3 + 4 = 7.
1 <= s.length <= 3 * 10^5s consists of integers and the operators '+', '-', '*', '/' separated by spacess represents a valid expressionAll intermediate results fit in a 32-bit signed integerInteger division truncates toward zero