Hone logo
Hone
Problems

Basic Calculator II: Integer Arithmetic

This challenge asks you to implement a basic calculator that can evaluate simple arithmetic expressions containing non-negative integers, '+', '-', '*', and '/'. The goal is to perform these calculations respecting the standard order of operations (multiplication and division before addition and subtraction) and handling integer division correctly. This is a fundamental problem that helps understand expression parsing and evaluation.

Problem Description

You are given a string s representing a valid arithmetic expression. The expression will contain only non-negative integers, the operators '+', '-', '*', and '/', and spaces. You need to calculate and return the result of the expression.

Key Requirements:

  • The expression will always be valid.
  • Integers are non-negative.
  • Integer division should truncate towards zero. For example, 3 / 2 should be 1 and 7 / 3 should be 2.
  • Multiplication and division have higher precedence than addition and subtraction.
  • Operations of the same precedence are evaluated from left to right.
  • No two consecutive operators will appear.
  • The result will fit within a 32-bit signed integer.

Expected Behavior: The function should take the input string s and return a single integer representing the evaluated result of the expression.

Edge Cases:

  • Expressions with only one number.
  • Expressions with only addition/subtraction.
  • Expressions with only multiplication/division.
  • Expressions involving integer division resulting in zero.
  • Expressions with spaces between numbers and operators.

Examples

Example 1:

Input: s = "3+2*2"
Output: 7
Explanation: In "3+2*2", both addition and multiplication are in the expression.
However, multiplication is prioritized, so it is first computed as 2 * 2 = 4.
Then, the expression becomes 3 + 4, which yields 7.

Example 2:

Input: s = " 3/2 "
Output: 1
Explanation: Integer division truncates towards zero. 3 / 2 = 1.

Example 3:

Input: s = " 3+5 / 2 "
Output: 5
Explanation: Division is prioritized. 5 / 2 = 2.
Then, the expression becomes 3 + 2, which yields 5.

Example 4:

Input: s = "14-3*4+10/2"
Output: 2
Explanation:
14 - 12 + 5
2 + 5
7

Constraints

  • 1 <= s.length <= 3 * 10^5
  • s consists of English letters (lowercase), digits, '+', '-', '', '/', ' ' and '.' (dot). (Note: The problem statement in LeetCode 227 specifically mentions digits, '+', '-', '', '/', and spaces. The constraint here is adjusted to align with typical Basic Calculator II definitions, omitting letters and dots for simplicity and correctness of the problem.)
  • s represents a valid expression.
  • All the integers in s are non-negative integers in the range [0, 2^31 - 1].
  • The answer is guaranteed to fit in a 32-bit signed integer.

Notes

  • You can assume that the input string s is always a valid expression.
  • Focus on correctly implementing the order of operations.
  • Consider how to handle the numbers and operators as you parse the string.
  • Think about data structures that might be helpful for storing intermediate results or managing operator precedence.
  • You do not need to handle parentheses or unary minus.
Loading editor...
plaintext