Explorer
Data Structures & Algorithms

Convert a string representation of an integer to a 32-bit signed integer.

Problem Statement

Implement the `myAtoi(string s)` function, which converts a string to a 32-bit signed integer. 1. Whitespace: Ignore leading whitespace. 2. Signedness: Determine sign with `+` or `-`. 3. Conversion: Read digits until next non-digit. 4. Rounding: Clamp to `[-2^31, 2^31 - 1]`.

Examples

Input: s = "42"

Output: 42

Explanation: Combining the input according to String to Integer (atoi) logic yields 42.

Input: s = " -42"

Output: -42

Explanation: Combining the input according to String to Integer (atoi) logic yields -42.

Input: s = "1337c0d3"

Output: 1337

Explanation: Combining the input according to String to Integer (atoi) logic yields 1337.

Complexity

Time Complexity: O(N)

Space Complexity: O(1)

Hints

šŸ’” Hint 1: Skip leading whitespace first. šŸ’” Hint 2: Check for an optional sign + or -. šŸ’” Hint 3: Read digits while checking for 32-bit signed integer overflow [-2^31, 2^31 - 1]. Stop at first non-digit.

Editorial & Approach

Problem Overview & Intuition

Simulate the standard C atoi specification: ignore whitespace, parse optional sign, accumulate numeric digits, clamp to [-2^31, 2^31 - 1] upon overflow, and terminate upon encountering any non-digit character.

Step-by-Step Approach

  1. Skip leading whitespace with i++.
  2. Read optional + or - sign.
  3. Read contiguous digit characters 0-9.
  4. Check for overflow against 32-bit boundaries.
  5. Return clamped signed result.

Optimal Implementation (JavaScript)

function myAtoi(s) {
  let i = 0, n = s.length, sign = 1, res = 0;
  while (i < n && s[i] === ' ') i++;
  if (i < n && (s[i] === '+' || s[i] === '-')) sign = s[i++] === '-' ? -1 : 1;
  const MAX = 2147483647, MIN = -2147483648;
  while (i < n && s[i] >= '0' && s[i] <= '9') {
    const d = s.charCodeAt(i++) - 48;
    if (res > Math.floor(MAX / 10) || (res === Math.floor(MAX / 10) && d > 7)) {
      return sign === 1 ? MAX : MIN;
    }
    res = res * 10 + d;
  }
  return sign * res;
}

Complexity Analysis

Time Complexity O(N) — single pass scanning digits.
Space Complexity O(1) — constant extra space.

Edge Cases & Corner Traps Handled

  • Empty string or all spaces: returns 0.
  • Sign followed by letters: returns 0.
  • Extreme positive/negative overflow.

String to Integer (atoi)

Medium
Implement the `myAtoi(string s)` function, which converts a string to a 32-bit signed integer. 1. Whitespace: Ignore leading whitespace. 2. Signedness: Determine sign with `+` or `-`. 3. Conversion: Read digits until next non-digit. 4. Rounding: Clamp to `[-2^31, 2^31 - 1]`.
Example Scenarios
1Example 1
Input: s = "42"
Output: 42
Explanation:

Combining the input according to String to Integer (atoi) logic yields 42.

2Example 2
Input: s = " -42"
Output: -42
Explanation:

Combining the input according to String to Integer (atoi) logic yields -42.

3Example 3
Input: s = "1337c0d3"
Output: 1337
Explanation:

Combining the input according to String to Integer (atoi) logic yields 1337.

Editor
Loading Editor...
s =
"42"
Output:Click "Run" above to execute and verify your code here.
42