Skip to content
Problem 227 · LeetCode 227(opens in a new tab)

Basic Calculator II

MediumMath

You get a string s that holds a valid arithmetic expression made of non-negative whole numbers and the operators +, -, * and /, with any number of spaces in between. Work out its value and return it.

The usual rules apply: * and / bind tighter than + and -, and operators of the same strength go left to right. Division keeps only the whole part, cutting toward zero. You must do the parsing yourself, with no built-in that evaluates expressions.

Example 1

Input:  s = "4+9/2*3"
Output: 16
Why:    9 / 2 cuts down to 4, 4 * 3 = 12, then 4 + 12
2 more examples and the constraints

Example 2

Input:  s = " 20 - 6 / 4 "
Output: 19
Why:    6 / 4 is 1, so 20 - 1; the spaces mean nothing

Example 3

Input:  s = "2*5+1"
Output: 11
Why:    the product comes first: 10 + 1

Constraints

  • 1 ≤ length of s ≤ 300,000
  • s holds digits, spaces and the four operators, and is always a valid expression
  • Every number is between 0 and 2,147,483,647
  • The answer and every partial result fit in a 32-bit signed integer

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Tokenize, then collapseO(n²)
  2. Stack of termsO(n)
  3. Running total and last termO(n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 300,000 charactersVerdict
Tokenize, then collapseO(n²)
Natural, but shifts the list
Stack of termsO(n)
Passes; easy to explain
Running total and last termO(n)
Best; same pass, no stack

n = length of s.

Edge cases to test

  • A single number. "42" has no operator at all; the extra + at the end still flushes it.
  • Spaces anywhere. " 20 - 6 / 4 " has spaces around numbers and operators; skip them, but do not end the number early.
  • Negative term divided. "1-7/2" is 1 − 3 = −2, not 1 − 4. The term is −7 and must truncate toward zero.
  • Large intermediate products. "65536*32768/2-1" multiplies to 2^31 before dividing; a long keeps that safe in Java and C++.

Common mistakes

  • Applying the operator just read instead of the one before the number, so 2*5+1 turns into 2 * (5 + 1).
  • Forgetting to flush the last number when the string ends.
  • Using floor division in Python, which gives the wrong answer for a negative term.

How to talk through it

  1. 1Clarify. Are there brackets or unary minus signs? Which way does division round? Can numbers have several digits?
  2. 2Describe terms. The value is a sum of terms; * and / live inside a term, + and - separate terms.
  3. 3Start with a stack. Push each term, rewrite the top for * and /, and sum at the end.
  4. 4Shrink to two variables. Only the top changes, so total and last replace the stack.
  5. 5Test out loud. Run "42", "1-7/2" and a string padded with spaces.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?