LeetCode
leetcode.com › problems › string-to-integer-atoi
String to Integer (atoi) - LeetCode
Can you solve this real interview question? String to Integer (atoi) - 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: 1. Whitespace: Ignore any leading ...
AlgoMonster
algo.monster › home › 8. string to integer (atoi)
8. String to Integer (atoi) - In-Depth Explanation
The clever part of this solution is the preemptive overflow check. By comparing against (2**31 - 1) // 10 and checking the last digit separately when equal, we avoid actual integer overflow in our calculation. This ensures the algorithm works correctly even in languages with fixed integer sizes. The time complexity is O(n) where n is the length of the string, as we traverse it at most once.
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
atoi the function used to convert a string to an integer...but...
I don't understand what you're asking at all. This function can only be used on a string that can be parsed as a series of decimal digits (optionally with a sign), not any arbitrary string. If you try to convert a string that's not in that format, then it fails and returns zero. That's also why this function is deprecated and should not be used, because you can't tell the difference between a successful conversion of the string representation of zero and the failure case of an invalid string. More on reddit.com
Atoi.
The atoi function is for converting a string (that is, char*) to an int. A char is not a string. The good news is that char is already a numeric type. You can just write int num = argv[1][k]; print("success: %d\n", num); Even if you do need to convert a string to an int, you shouldn't use atoi, because it lacks error handling. You can pass in "flurbl" and it will still return an int value. Instead you should use strtol or sscanf. In the former case you need to set errno=0 first, in order to distinguish overflow from a correct conversion of INT_MAX or INT_MIN. More on reddit.com
Atoi
atoi converts a string to a number. It will not work with individual characters, only with a null-terminated string. You seem to be trying to convert individual characters of argv[1] to integers, rather than trying to convert argv[1] itself. Note that you should only try to convert argv[1] after you have checked all characters of argv[1] to make sure that they are valid digits. More on reddit.com
14:55
LeetCode: String To Integer (atoi) - YouTube
19:07
Google Engineer Explains - LeetCode #8 - String to Integer (atoi) ...
10:00
Amazon interview question | String to Integer (atoi) | leetcode ...
Coding Interview Tutorial 66: String to Integer [LeetCode]
18:45
LeetCode Tutorial 8. String to Integer (atoi) - YouTube
GeeksforGeeks
geeksforgeeks.org › dsa › write-your-own-atoi
String to Integer - Write your own atoi() - GeeksforGeeks
Write your own atoi() that takes a string (which represents a floating point value) as an argument and returns its value as double.
Published: April 14, 2026
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 - Prior to taking care of the numeric case, care has to be taken care in order to ensure that the current character is not out of the asci range of ‘0’ or ‘9’. If it so, then according to the given rule, we break from the string iteration to return the result, subject to certain conditions. 5 If isNegative is true, then we toggle the result to point to the negative version of its value. 6 If result which is of type double, is less than Integer.MIN_VALUE, then we return Integer.MIN_VALUE.
WalkCCC
walkccc.me › LeetCode › problems › 8
8. String to Integer (atoi) - LeetCode Solutions
LeetCode Solutions in C++23, Java, Python, MySQL, and TypeScript.
Red Quark
redquark.org › leetcode › 0008-string-to-integer-atoi
LeetCode #8 - String To Integer (atoi) | Red Quark
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.
Reddit
reddit.com › r/programminghorror › after 3 hours of pain and misery, my solution to “string to integer” on leetcode
r/programminghorror on Reddit: After 3 hours of pain and misery, my solution to “String to Integer” on LeetCode
January 4, 2024 -
I kept getting stupid test cases and the description was incredibly unspecific, so even though I switched over to python my code ended up a giant, mangled, unreadable monstrosity due to adding so many parameters
Top answer 1 of 5
898
Your approach to taking screenshots is deeply disturbing.
2 of 5
634
I don't think this should be that hard. EDIT: This is what I would have done: Remove leading and trailing whitespace Check first symbol for + or - to know integer sign, and remove that symbol (not if it is a digit) Assign accumulator acc = 0 Start from leftmost digit, convert it to integer ("9" -> 9) then add into an accumulator variable like acc = acc*10 + digit, loop till all digits Make accumulator negative if negative sign was present Return accumulator
DEV Community
dev.to › jaspreet_singh_86ae1740ac › string-to-integer-atoi-2nki
String to Integer (atoi) - DEV Community
June 30, 2026 - // Trim spaces // Store digits into StringBuilder // Convert using Integer.parseInt() // Handle exceptions for overflow ... Build the answer digit by digit. ... Skip leading spaces. Determine sign. ... Process every digit. ... class Solution { public int myAtoi(String s) { int i = 0; int n = s.length(); while (i < n && s.charAt(i) == ' ') i++; int sign = 1; if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) { if (s.charAt(i) == '-') sign = -1; i++; } int number = 0; while (i < n && Character.isDigit(s.charAt(i))) { int digit = s.charAt(i) - '0'; if (number > (Integer.MAX_VALUE - digit) / 10) { return sign == 1 ?
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 - Check Bounds: As the number is built, ensure it stays within the 32-bit integer limits, clamping to int.MaxValue or int.MinValue if necessary. Apply the Sign: Multiply the result by the sign to get the correct positive or negative value. This approach is clean, avoids heavy string operations, and handles all edge cases efficiently. class Solution { public int MyAtoi(string s) { // Handle null or empty string if (string.IsNullOrEmpty(s)) return 0; int idx = 0, end = s.Length; // Skip leading whitespace while (idx < end && s[idx] == ' ') idx++; if (idx == end) return 0; // Return 0 if only whitespace // Check for sign int sign = 1; // Default to positive if (s[idx] == '+' || s[idx] == '-') { sign = (s[idx] == '-') ?
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
Theresearchguy
theresearchguy.com › home › string to integer (atoi)
String to Integer (atoi) – theresearchguy.com
April 13, 2026 - public int myAtoi(String s) { int i = 0, n = s.length(), sign = 1; long result = 0; while (i < n && s.charAt(i) == ' ') i++; if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) { sign = s.charAt(i) == '-' ? -1 : 1; i++; } while (i < n && Character.isDigit(s.charAt(i))) { result = result * 10 + (s.charAt(i) - '0'); if (result * sign > Integer.MAX_VALUE) return Integer.MAX_VALUE; if (result * sign < Integer.MIN_VALUE) return Integer.MIN_VALUE; i++; } return (int)(result * sign); }
takeuforward
takeuforward.org › plus › dsa › problems › string-to-integer-atoi
String to Integer (atoi) - TUF+
Search for a command to run
GhostInterview
ghostinterview.co › home › leetcode problems › string to integer (atoi)
String to Integer (atoi) LeetCode Problem Guide | GhostInterview
March 8, 2026 - This ensures precise extraction from a string-driven input. While accumulating digits, check if adding the next digit would exceed the 32-bit signed integer range. If overflow is detected, clamp the result to INT_MAX or INT_MIN depending on the sign. This prevents common failure modes in atoi conversions.