Tap here to switch tabs
Problem
Submissions

Problem: Basic Calculator II

Basic Calculator II asks you to evaluate a valid expression string with non-negative integers and addition, subtraction, multiplication, and division, truncating division toward zero.

Statement

Given a string s representing a mathematical expression containing non-negative integers and the operators +, -, *, and /, evaluate the expression and return its result.

Integer division must truncate toward zero. You may assume the expression is always valid, and all intermediate results fall within the 3232-bit signed integer range.

Note: Using any built-in function that directly evaluates strings as mathematical expressions (e.g., eval()) is not allowed.

Constraints:

  • 1≤1 \leq s.length ≤3×105\leq 3 \times 10^5

  • s consists of non-negative integers and operators ('+', '-', '*', '/') separated by some number of spaces

  • s represents a valid expression

  • All integers in s are in the range [0,231−1][0, 2^{31} - 1]

  • The answer is guaranteed to fit within a 3232-bit signed integer

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Basic Calculator II

Basic Calculator II asks you to evaluate a valid expression string with non-negative integers and addition, subtraction, multiplication, and division, truncating division toward zero.

Statement

Given a string s representing a mathematical expression containing non-negative integers and the operators +, -, *, and /, evaluate the expression and return its result.

Integer division must truncate toward zero. You may assume the expression is always valid, and all intermediate results fall within the 3232-bit signed integer range.

Note: Using any built-in function that directly evaluates strings as mathematical expressions (e.g., eval()) is not allowed.

Constraints:

  • 1≤1 \leq s.length ≤3×105\leq 3 \times 10^5

  • s consists of non-negative integers and operators ('+', '-', '*', '/') separated by some number of spaces

  • s represents a valid expression

  • All integers in s are in the range [0,231−1][0, 2^{31} - 1]

  • The answer is guaranteed to fit within a 3232-bit signed integer