DEV Community

Mahdi Shamlou | Solving LeetCode #8: String to Integer (atoi) — Step by Step

Hey everyone! Mahdi Shamlou here — continuing my LeetCode classic problems series 🚀

After [#7 Reverse Integer (See the list that I solved)], today I jumped into Problem #8 — String to Integer (atoi).

Mahdi Shamlou | مهدی شاملو

At first glance, this one looks easy… but once you start reading the rules carefully, you realize it’s all about edge cases 😅

Problem Statement:

Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer.

The algorithm should:

  1. Ignore leading whitespace
  2. Check for an optional + or - sign
  3. Read digits until a non-digit is found
  4. Clamp the result to the 32-bit signed integer range: [-²³¹, ²³¹ − 1]
  5. Return 0 if no valid conversion exists

Examples:

Input: "42"
Output: 42
----
Input: "   -42"
Output: -42
----
Input: "4193 with words"
Output: 4193
----
Input: "words and 987"
Output: 0
Enter fullscreen mode Exit fullscreen mode

My Thought Process

When I started, I told myself:

Okay… don’t panic. Just follow the rules one by one. just do it !

So I broke the problem into simple steps:

  1. Skip all leading whitespaces
  2. Detect the sign (+ or -)
  3. Read digits and build the number
  4. Handle overflow before it happens

That last part is super important — Python won’t overflow, but LeetCode expects 32-bit behavior.

My Solution (Clean & Safe)

class Solution:
    def myAtoi(self, s: str) -> int:
        """
        first of all i think i can just jump empty section of s

        """
        # Step 1: Skip whitespaces
        i = 0
        n = len(s)
        while i < n and s[i] == ' ':
            i += 1

        if i == n:
            return 0

        # Step 3: find sign
        sign = +1
        if s[i] == '-':
            sign = -1
            i += 1
        elif s[i] == '+':
            i += 1

        # Step 4: read the s for findig ints 
        result = 0
        INT_MAX_OF_OVERFELLOW = 2**31 - 1   # 2147483647
        INT_MIN_OF_OVERFELLOW = -2**31      # -2147483648

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


            if result > (INT_MAX_OF_OVERFELLOW - digit) // 10:
                return INT_MAX_OF_OVERFELLOW if sign == 1 else INT_MIN_OF_OVERFELLOW

            result = result * 10 + digit
            i += 1


        return result * sign
Enter fullscreen mode Exit fullscreen mode

sorry my comments not okay :)

Why I Like This Approach

  • ️ Easy to read
  • ️ Matches the problem rules exactly
  • ️ Handles all edge cases

Final Thoughts

This problem taught me something important:

Sometimes the “hard” part isn’t the algorithm — it’s discipline.

Reading carefully.
Handling edge cases.
Not rushing.

And yes… after passing all test cases in first time, I had that quick “okay, nice” moment — and then I jumped straight to the next problem 🚀

Mahdi Shamlou | مهدی شاملو

My Repo (All Solutions)


GitHub — mahdi0shamlou/LeetCode

What about you?

How did you approach this problem?
Did you miss any edge case the first time?

Drop your thoughts — I read everything! 🚀
Connect with me

🔗 LinkedIn:
https://www.linkedin.com/in/mahdi-shamlou-3b52b8278

📱 Telegram:
https://telegram.me/mahdi0shamlou

📸 Instagram:
https://www.instagram.com/mahdi0shamlou/

Author: Mahdi Shamlou | مهدی شاملو

Top comments (0)