Skip to content

8. String to Integer (atoi) (Medium)

Pyodide

Editor (session: default) Run
# Type here

Output Clear

Run

Editor (session: default) Run
class Solution:
    def myAtoi(self, s: str) -> int:

        s = s.lstrip()
        if not s:
            return 0

        sign = 1
        i = 0

        if s[0] == '+':
            i += 1
        elif s[0] == '-':
            sign = -1
            i += 1

        result = 0
        INT_MAX = 2147483647
        INT_MIN = -2147483648

        while i < len(s) and s[i].isdigit():
            digit = int(s[i])

            if sign == 1:
                if result > INT_MAX // 10 or (result == INT_MAX // 10 and digit > 7):
                    return INT_MAX
            else:
                if result > INT_MAX // 10 or (result == INT_MAX // 10 and digit > 8):
                    return INT_MIN

            result = result * 10 + digit
            i += 1

        return result * sign

# --- Example Usage Code ---

sol = Solution()
print("--- String to Integer (atoi) Examples ---")

# Test Case 1: Standard positive conversion
s1 = "42"
result1 = sol.myAtoi(s1)
print(f"Input: '{s1}'")
print(f"Output: {result1} (Expected: 42)")

# Test Case 2: Conversion with leading whitespace and sign
s2 = "   -42"
result2 = sol.myAtoi(s2)
print(f"Input: '{s2}'")
print(f"Output: {result2} (Expected: -42)")

# Test Case 3: Conversion with non-digit characters after the number
s3 = "4193 with words"
result3 = sol.myAtoi(s3)
print(f"Input: '{s3}'")
print(f"Output: {result3} (Expected: 4193)")

# Test Case 4: No valid number found
s4 = "words and 987"
result4 = sol.myAtoi(s4)
print(f"Input: '{s4}'")
print(f"Output: {result4} (Expected: 0)")

# Test Case 5: Positive Overflow (Should return INT_MAX: 2147483647)
s5 = "91283472332"
result5 = sol.myAtoi(s5)
print(f"Input: '{s5}'")
print(f"Output: {result5} (Expected: 2147483647)")

# Test Case 6: Negative Overflow (Should return INT_MIN: -2147483648)
s6 = "-91283472332"
result6 = sol.myAtoi(s6)
print(f"Input: '{s6}'")
print(f"Output: {result6} (Expected: -2147483648)")

# Test Case 7: Only sign
s7 = "+"
result7 = sol.myAtoi(s7)
print(f"Input: '{s7}'")
print(f"Output: {result7} (Expected: 0)")

# Test Case 8: Exact positive boundary
s8 = "2147483647"
result8 = sol.myAtoi(s8)
print(f"Input: '{s8}'")
print(f"Output: {result8} (Expected: 2147483647)")

# Test Case 9: Exact negative boundary
s9 = "-2147483648"
result9 = sol.myAtoi(s9)
print(f"Input: '{s9}'")
print(f"Output: {result9} (Expected: -2147483648)")
Output Clear

Solution

class Solution:
    def myAtoi(self, s: str) -> int:

        s = s.lstrip()
        if not s:
            return 0

        sign = 1
        i = 0

        if s[0] == '+':
            i += 1
        elif s[0] == '-':
            sign = -1
            i += 1

        result = 0
        INT_MAX = 2147483647
        INT_MIN = -2147483648

        while i < len(s) and s[i].isdigit():
            digit = int(s[i])

            if sign == 1:
                if result > INT_MAX // 10 or (result == INT_MAX // 10 and digit > 7):
                    return INT_MAX
            else:
                if result > INT_MAX // 10 or (result == INT_MAX // 10 and digit > 8):
                    return INT_MIN

            result = result * 10 + digit
            i += 1

        return result * sign

Function Description

Source code in python/_8.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution:
    def myAtoi(self, s: str) -> int:

        s = s.lstrip()
        if not s:
            return 0

        sign = 1
        i = 0

        if s[0] == '+':
            i += 1
        elif s[0] == '-':
            sign = -1
            i += 1

        result = 0
        INT_MAX = 2147483647
        INT_MIN = -2147483648

        while i < len(s) and s[i].isdigit():
            digit = int(s[i])

            if sign == 1:
                if result > INT_MAX // 10 or (result == INT_MAX // 10 and digit > 7):
                    return INT_MAX
            else:
                if result > INT_MAX // 10 or (result == INT_MAX // 10 and digit > 8):
                    return INT_MIN

            result = result * 10 + digit
            i += 1

        return result * sign

myAtoi(s)

Source code in python/_8.py
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def myAtoi(self, s: str) -> int:

    s = s.lstrip()
    if not s:
        return 0

    sign = 1
    i = 0

    if s[0] == '+':
        i += 1
    elif s[0] == '-':
        sign = -1
        i += 1

    result = 0
    INT_MAX = 2147483647
    INT_MIN = -2147483648

    while i < len(s) and s[i].isdigit():
        digit = int(s[i])

        if sign == 1:
            if result > INT_MAX // 10 or (result == INT_MAX // 10 and digit > 7):
                return INT_MAX
        else:
            if result > INT_MAX // 10 or (result == INT_MAX // 10 and digit > 8):
                return INT_MIN

        result = result * 10 + digit
        i += 1

    return result * sign

Explanation

The "String to Integer (atoi)" problem requires implementing the logic to convert a string into a 32-bit signed integer, adhering to specific rules. The function should first discard any leading whitespace characters until the first non-whitespace character is found. Next, this character may be an optional sign ('+' or '-'). Finally, the function must read in as many numerical digits as possible until a non-digit character is encountered or the end of the input string is reached. The resulting digits are then interpreted as an integer.

A crucial part of the implementation is handling all possible edge cases and constraints. If the first non-whitespace character is not a sign or a digit, no valid conversion can be performed, and \(0\) should be returned. The problem requires clamping the result to fit within the range of a 32-bit signed integer, which is \([-(2^{31}), 2^{31} - 1]\). If the numerical value exceeds the maximum limit, the function must return \(2^{31} - 1\) (INT_MAX). If it falls below the minimum limit, it must return \(-(2^{31})\) (INT_MIN).

The conversion itself is best done by iterating through the numeric digits, similar to problem 7, and continuously building the result while simultaneously checking for overflow or underflow at each step. This process involves multiplying the current result by \(10\) and adding the next digit. To ensure \(O(1)\) space complexity, the use of auxiliary data structures should be avoided. The overall time complexity for this process is \(O(n)\), where \(n\) is the length of the string, as we only perform a single pass over the string.