← All problems

String to Integer (atoi)

Medium stringsmathsimulation LeetCode ↗
Implement atoi: parse a string to a 32-bit signed integer, skipping leading whitespace, handling an optional sign, stopping at the first non-digit, and clamping overflow to [-2^31, 2^31 - 1].

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(n), Space O(1)

Common pitfalls

  • Overflow after the fact
  • Sign applied wrong
  • Trailing chars mishandled

Your solution

Log in to save your code and track progress.