Basic Calculator II
MediumP2Coding
Evaluate a simple arithmetic expression given as a string and return its value as an integer.
The string contains non-negative integers, the operators +, -, *, / and spaces. There are no parentheses and no unary minus. Integer division truncates toward zero. Multiplication and division bind tighter than addition and subtraction. The expression is always valid. Do not use eval.
Example 1
- Input
- s = "3+2*2"
- Output
- 7
Example 2
- Input
- s = " 3/2 "
- Output
- 1
Example 3
- Input
- s = " 3+5 / 2 "
- Output
- 5
Constraints
- 1 <= s.length <= 10^5
- Operands are integers in [0, 2^31 - 1]
- All intermediate results fit in a 32-bit signed integer
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 12. ⌘Enter runs, ⌘⇧Enter submits.