🌐
LeetCode
leetcode.com › problems › string-to-integer-atoi
String to Integer (atoi) - LeetCode
8. String to Integer (atoi) Medium · Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer. The algorithm for myAtoi(string s) is as follows: Whitespace: Ignore any leading whitespace (" "). Signedness: ...
Explore
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
Top Interview 150
Must-do List for Interview Prep
Premium
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
🌐
AlgoMonster
algo.monster › home › 8. string to integer (atoi)
8. String to Integer (atoi) - In-Depth Explanation
The problem asks you to implement a function myAtoi(string s) that converts a string to a 32-bit signed integer, similar to C/C++'s atoi function.
Discussions

After 3 hours of pain and misery, my solution to “String to Integer” on LeetCode
Your approach to taking screenshots is deeply disturbing. More on reddit.com
🌐 r/programminghorror
169
2245
January 4, 2024
How is this test case valid?

Rule number 1: If the like to dislike ratio is less than 1, don’t solve it

More on reddit.com
🌐 r/leetcode
9
0
July 13, 2023
🌐
GitHub
github.com › doocs › leetcode › blob › main › solution › 0000-0099 › 0008.String to Integer (atoi) › README_EN.md
leetcode/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md at main · doocs/leetcode · GitHub
Conversion: Read the integer by skipping leading zeros until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0. Rounding: If the integer is out of the 32-bit signed integer ...
Author: doocs
🌐
WalkCCC
walkccc.me › LeetCode › problems › 8
8. String to Integer (atoi) - LeetCode Solutions
LeetCode Solutions in C++23, Java, Python, MySQL, and TypeScript.
🌐
Medium
medium.com › @rutujakbhombe19 › solving-leetcode-until-i-land-a-job-306a30914738
Leetcode 8 : String to Integer (atoi) | by Rutuja Bhombe | Medium
July 17, 2024 - Read each character until a non-digit character is encountered or the end of the string is reached. Convert these characters into a number. ... If the number is outside the range of a 32-bit signed integer, clamp it to the range limits.
🌐
PREP INSTA
prepinsta.com › home › leetcode top 100 liked questions with solution › string to integer (atoi) leetcode solution
String to Integer (atoi) Leetcode Solution
January 9, 2024 - Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored. Convert these digits into an integer (i.e. “123” -> 123, “0032” -> 32).
🌐
DEV Community
dev.to › grantdotdev › leetcode-8-string-to-integer-atoi-medium-33om
LeetCode 8: String to Integer (atoi) - Medium - DEV Community
September 21, 2025 - LeetCode's String to Integer (atoi) is a classic medium-difficulty coding problem that challenges you to convert a string representation of a number into an actual integer.
🌐
PROGIEZ
progiez.com › 8-string-to-integer-atoi-leetcode-solution
8. String To Integer (atoi) LeetCode Solution - Progiez
September 12, 2024 - class Solution { public int myAtoi(String s) { s = s.strip(); if (s.isEmpty()) return 0; final int sign = s.charAt(0) == '-' ? -1 : 1; if (s.charAt(0) == '+' || s.charAt(0) == '-') s = s.substring(1); long num = 0; for (final char c : s.toCharArray()) { if (!Character.isDigit(c)) break; num = num * 10 + (c - '0'); if (sign * num <= Integer.MIN_VALUE) return Integer.MIN_VALUE; if (sign * num >= Integer.MAX_VALUE) return Integer.MAX_VALUE; } return sign * (int) num; } } // code provided by PROGIEZ · class Solution: def myAtoi(self, s: str) -> int: s = s.strip() if not s: return 0 sign = -1 if s[0] == '-' else 1 if s[0] in {'-', '+'}: s = s[1:] num = 0 for c in s: if not c.isdigit(): break num = num * 10 + int(c) if sign * num <= -2**31: return -2**31 if sign * num >= 2**31 - 1: return 2**31 - 1 return sign * num #code by PROGIEZ · Explore all Leetcode problems solutions at Progiez here
Find elsewhere
🌐
Red Quark
redquark.org › leetcode › 0008-string-to-integer-atoi
LeetCode #8 - String To Integer (atoi) | Red Quark
fun myAtoi(s: String): Int { var str = s // Base condition if (str.isEmpty()) { return 0 } // MAX and MIN values for integers val max = 2147483647 val min = -2147483648 // Trimmed string str = str.replace("^\\s+".toRegex(), "") // Counter var i = 0 // Flag to indicate if the number is negative val isNegative: Boolean = str.startsWith("-") // Flag to indicate if the number is positive val isPositive: Boolean = str.startsWith("+") if (isNegative) { i++ } else if (isPositive) { i++ } // This will store the converted number var number = 0.0 // Loop for each numeric character in the string iff nume
🌐
Doocs
leetcode.doocs.org › en › lc › 8
8. String to Integer (atoi) - LeetCode Wiki
Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer.
🌐
Gitbook
zhenchaogan.gitbook.io › leetcode-solution › leetcode-8-string-to-integer-atoi
LeetCode 8. String to Integer (atoi) | leetcode solution
For the complete documentation index, see llms.txt. This page is also available as Markdown. ... Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function).
🌐
Medium
medium.com › @harycane › string-to-integer-atoi-1e75325c5fd7
String to Integer (atoi). Implement atoi which converts a string… | by Hary Krishnan | Medium
August 1, 2018 - String to Integer (atoi) String to Integer (atoi) - LeetCode Implement atoi which converts a string to an integer. The function first discards as many whitespace characters …
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › write-your-own-atoi
String to Integer - Write your own atoi() - GeeksforGeeks
Traverse the string from left to right, skipping whitespaces, handling the optional sign, and building the number digit by digit. At each step, ensure the value stays within 32-bit integer limits to avoid overflow.
Published: April 14, 2026
🌐
Medium
medium.com › @zzhenyyang › string-to-integer-atoi-leetcode-36061f577eff
String to Integer (atoi) — LeetCode | by Zhen Yang | Medium
October 17, 2024 - /** * @param {string} s * @return {number} */ var myAtoi = function(s) { let i = 0; let sign = 1; let output = 0; const INT_MIN = Math.pow(2, 31) * -1; const INT_MAX = Math.pow(2, 31) - 1; // Skip leading spaces while (i < s.length && s[i] === ...
🌐
Medium
abdullahniaz.medium.com › leetcode-problem-8-string-to-integer-atoi-2af6971d9cc9
LeetCode Problem: 8. String to Integer (atoi) | by Abdullah Niaz | Medium
January 23, 2025 - LeetCode Problem: 8. String to Integer (atoi) Problem Statement The task is to implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer. The conversion process must …
🌐
Medium
medium.com › @soham-khade › 8-string-to-integer-atoi-leetcode-problem-java-042a26831f49
8. String to Integer (atoi) LeetCode Problem (Java) | by Soham | Medium
October 27, 2023 - Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored. Convert these digits into an integer (i.e. "123" -> 123, "0032" -> 32).
🌐
Gitlab
memorylimitexceeded.gitlab.io › leetcode › problems › 0008-string-to-integer-atoi.html
8. String to Integer (atoi) | Leetcode Solutions
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
Top answer
1 of 1
2

Normally when parsing a String into an int, people expect it to be a static method

Having to create a class to do the computation is a bit bulky, and then you need to worry about synchronization (what happens if this class is shared across multiple threads?)

boolean isOverflow(int prev, int curr) {
    // prev = value at last iteration
    // curr = value after current iteration
    if (curr/10 == prev) {
        return false;
    }
    return true;
}

This method is already a great candidate to become static, it only relies on its two inputs to figure out if an overflow happened

boolean isNegative = false;
int i = 0; // string iterator position

void checkSign(String str) //uses i and isNegative

int signSolution(int solution) //uses isNegative

These are slightly more tricky, int signSolution(int solution) can just take another parameter: static int signSolution(int solution, boolean isNegative)

void checkSign(String str) would need to be included as part of the myAtoI method or be rewritten a bit

If we treat + as whitespace, then it would only need to worry about handling -

static boolean checkSign(String str, int i)
{
    return str.charAt(i) == '-';
}

And calls to checkSign(str); would instead be if(checkSign(str)) i++;


Be consistent. You're mixing for and while loops of similar structure

while (i < str.length() && str.charAt(i) == ' ')
    i++;
[...]
for (; i < str.length(); i++) {
    [...]
}

They can either both be for loops, or both be while loops, but mixing is unexpected

for(; i < str.length() && str.charAt(i) == ' '; i++);
[...]
for(; i < str.length(); i++)
{
    [...]
}

vs

while(i < str.length() && str.charAt(i) == ' ')
    i++;
[...]
while(i < str.length())
{
    [...]
    i++;
}

Checking "whitespace" currently only checks spaces

Instead of using str.charAt(i) == ' ', you can offload complicated functionality to another method (and be easier to change later)

static boolean isWhiteSpace(char c)
{
    return c == ' ';
}

Then if you'd like to include all whitespace (and + and mentioned above)

static boolean isWhiteSpace(char c)
{
    return Character.isWhitespace(c) || (c == '+');
}

Instances of if(bool) return true; else return false; are the same as just return bool;

if (curr/10 == prev) {
        return false;
}
return true;

Can be simplified to just

return curr / 10 != prev;

OptionalA:

Another way to check for overflow is to store the output as a long, and check if it overflows

public static int myAtoI(String str)
{
    long solution = 0;
    [...]
}

private static isOverflow(long num)
{
    return num == (int)num;
}

OptionalB:

Instead of adding the values and then flipping the result, you could subtract values if it's a negative number

🌐
LeetCode
leetcode.ca › all › 8.html
8. String to Integer (atoi) - leetcode.ca
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.