LeetCode
leetcode.com › problems › string-to-integer-atoi
8. String to Integer (atoi)
Return the integer as the final result. Note: You must not use any built-in library function that converts a string to a number (for example, atoi, stoi, Integer.parseInt, int(), parseInt, Number()).
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
Boost your coding interview skills and confidence by practicing real interview questions with LeetCode. Our platform offers a range of essential problems for practice, as well as the latest questions being asked by top-tier companies.
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.
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
WalkCCC
walkccc.me › LeetCode › problems › 8
8. String to Integer (atoi) - LeetCode Solutions
LeetCode Solutions in C++23, Java, Python, MySQL, and TypeScript.
AlgoMonster
algo.monster › home › 8. string to integer (atoi)
8. String to Integer (atoi) - In-Depth Explanation
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, ...
YouTube
youtube.com › watch
String to Integer atoi 🔥| Leetcode 8 | String - YouTube
Timestamps:Problem explanation: 00:00Approaching the problem: 04:14Dry Run: 06:53Code Explanation: 13:15Complexity Analysis: 16:38Time Complexity : O(n)Space...
Published: January 14, 2022
YouTube
youtube.com › watch
STRING TO INTEGER (ATOI) | PYTHON | LEETCODE # 8 - YouTube
In this video we are solving a popular interview question at top tech companies Google, Facebook, Microsoft, and others. This is a pretty simple question but...
Published: March 2, 2022
Gitbooks
frankchen0521.gitbooks.io › leetcode-swift › content › 8string-to-integer-atoi.html
8.String to Integer (atoi) · LeetCode - Swift - Frank Chen
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned. ... class Solution { func myAtoi(_ str: String) -> Int { let sign:[Character] = ["+", "-"]; let ...
codingcircle
codingcircle.in › 8-string-to-integer-atoi
codingcircle » 8. String to Integer (atoi)
February 22, 2025 - Read the subsequent characters until a non-digit is encountered or the end of the string is reached. These characters represent the numeric part of the string. Convert the sequence of digits into an integer.
DEV Community
dev.to › aaradhyanegi009 › leetcode-solution-8-string-to-integer-atoi-2jli
LeetCode Solution: 8. String to Integer (atoi) - DEV Community
May 18, 2026 - Since we handled all overflows during step 3, this final cast will safely return the correct, clamped 32-bit integer. ... Combining all these steps gives us a complete and robust atoi function! #include <string> // For std::string #include <climits> // For INT_MAX, INT_MIN #include <cctype> // For isdigit (good practice to include explicitly) class Solution { public: int myAtoi(std::string s) { int i = 0; // Step 1: Skip leading whitespace while (i < s.length() && s[i] == ' ') { i++; } // Step 2: Determine sign int sign = 1; // Assume positive initially if (i < s.length() && (s[i] == '-' || s[i] == '+')) { sign = (s[i] == '-') ?
Red Quark
redquark.org › leetcode › 0008-string-to-integer-atoi
LeetCode #8 - String To Integer (atoi) | Red Quark
We are not using any data structure ... if (str == null || str.length() < 1) { return 0; } // MAX and MIN values for integers final int INT_MAX = 2147483647; final int INT_MIN = -2147483648; // Trimmed string str = s...
YouTube
youtube.com › watch
String To Integer (atoi) - 8. LeetCode - Java - YouTube
String To Integer (atoi) - 8. LeetCode - Java Github repo: https://github.com/teddysmithdev/LeetCodeJava2024LeetCode description: https://leetcode.com/proble...
Published: August 20, 2024
Top answer 1 of 3
5
Nice solution, it's compact and simple to understand. There is little to improve, few suggestions:
- Imports: there are a lot of imports, maybe leftovers of previous attempts.
- Try Except block: should be around the code that can cause the exception.
Applying the suggestions:
import re
class Solution:
def myAtoi(self, s: str) -> int:
MAX, MIN = 2147483647, -2147483648
s = re.findall(r'^\s*[+-]?\d+', s)
try:
res = int(''.join(s))
except:
return 0
if res > MAX:
return MAX
if res < MIN:
return MIN
return res
Performance
Runtime: 36 ms, faster than 51.56% of Python3 online submissions
Memory Usage: 14.1 MB, less than 32.27% of Python3 online submissions
Regex makes the code compact but is not the fastest approach. Faster solutions iterate through the string character by character.
2 of 3
4
Magic numbers
2147483647, -2147483648
are actually
1<<31 - 1, -(1<<31)
which better conveys your intent: the limits of a signed 32-bit integer.
Pre-compile your regex
Consider putting a
DIGIT_PATTERN = re.compile(r'^\s*[+-]?\d+')
in global scope, so that multiple calls to myAtoi are faster.
Never bare except
You should likely instead except ValueError, which is more narrow and well-defined.
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.
Paul Coroneos
pcoroneos.com › blog › atoi
Solving Leetcode 8 - String to Integer (atoi) (JavaScript) - Paul Coroneos
July 29, 2020 - 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.
YouTube
youtube.com › watch
LeetCode Tutorial 8. String to Integer (atoi) - YouTube
GoodTecher LeetCode Tutorial 8. String to Integer (atoi) (Java). http://www.goodtecher.com/leetcode-8-string-integer-atoi-java/LeetCode Tutorial by GoodTeche...
Published: February 24, 2017