Home
DSA Patterns
Two Pointers
String to Integer (atoi)
String to Integer (atoi)
Convert a string to a 32-bit signed integer the way C's atoi does: skip leading spaces, read an optional sign and digits, and clamp on overflow.
Problem Understanding
String to Integer (atoi) (LeetCode 8): implement myAtoi(s). Ignore leading spaces; read an optional + or -; read digits until a non-digit or the end of the string, ignoring leading zeros. If no digits were read, return 0. Clamp the result to the 32-bit range [-2^31, 2^31 - 1].
Examples: "42" → 42; " -042" → -42; "1337c0d3" → 1337; "0-1" → 0; "words and 987" → 0.
The Intuition: Rules in Order, One Index
atoi is a small state machine you can write as three loops in a row, all moving the same index.
- Spaces: skip them.
- Sign: at most one
+or-. - Digits: build the number left to right, result = result × 10 + digit, and stop at the first character that is not a digit.
The only subtle part is overflow. Check before you multiply, so the value never leaves the 32-bit range. The digit-by-digit building is the same as Add Strings, and the overflow guard is the one used in Reverse Integer.
Interactive Walkthrough
RULES IN ORDER
START AT 0
s
0
1
2
3
4
5
6
result = 0
Sign
+
i
:
Digits
0
Start at i = 0. Result 0, sign +1. The limits are -2147483648 and 2147483647
Next
s[0] is a space: skip it
s
Spaces are drawn as ␣ so you can see them being skipped. Try Example 4: the - after the 0 is not a sign, it just ends the number. Overflow stops one digit early and returns the limit.
Dry Run Table
Input: s = " -042".
| i | s[i] | Rule | sign | result |
|---|---|---|---|---|
| 0 to 2 | space | skip leading spaces | +1 | 0 |
| 3 | - | read the sign | -1 | 0 |
| 4 | 0 | digit: 0 × 10 + 0 | -1 | 0 |
| 5 | 4 | digit: 0 × 10 + 4 | -1 | 4 |
| 6 | 2 | digit: 4 × 10 + 2 | -1 | 42 |
The string ends. Answer: -1 × 42 = -42. The leading zero is simply absorbed.
The Solution Template
One index and three loops. The overflow check compares with INT_MAX / 10 before multiplying, so it is safe in fixed-width integers.
String to Integer (atoi) Code
1var myAtoi = function(s) {2 const INT_MAX = 2 ** 31 - 1, INT_MIN = -(2 ** 31);3 let i = 0;4 const n = s.length;5 while (i < n && s[i] === ' ') {6 i++;7 }8 let sign = 1;9 if (i < n && (s[i] === '+' || s[i] === '-')) {10 sign = s[i] === '-' ? -1 : 1;11 i++;12 }13 let result = 0;14 while (i < n && s[i] >= '0' && s[i] <= '9') {15 const digit = s.charCodeAt(i) - 48;16 if (result > Math.floor(INT_MAX / 10) || (result === Math.floor(INT_MAX / 10) && digit > 7)) {17 return sign === 1 ? INT_MAX : INT_MIN;18 }19 result = result * 10 + digit;20 i++;21 }22 return sign * result;23};
Edge Cases & Common Mistakes
- Empty or all spaces: no digits, return 0.
- Two signs, like "+-12": only one sign is read; the second stops the parse, so the answer is 0.
- Spaces after the sign: "- 12" is 0, because spaces are only skipped at the very start.
- Overflow: checking after result * 10 + digit overflows a 32-bit int first; check before.
- Leading zeros: "0000012" is 12; they add nothing.
- Using a library parser: most of them accept or reject different inputs than atoi, so write the rules out.
Summary
Skip leading spaces, read at most one sign, then read digits while guarding against overflow before each multiply. Stop at the first non-digit and return sign * result. O(n) time, O(1) space.
More practice: DSA interview questions with answers · patterns cheat sheet
