AlgoMonster
algo.monster › home › 8. string to integer (atoi)
8. String to Integer (atoi) - In-Depth Explanation
Skip leading whitespace: Any spaces at the beginning of the string should be ignored. Determine the sign: After skipping whitespace, check if the next character is '-' or '+'. If it's '-', the number will be negative. If it's '+' or neither sign is present, the number will be positive. Read digits: Convert consecutive digit characters into an integer.
Dashboard
Coding interviews stressing you out? Get the structure you need to succeed. Get Interview Ready In 6 Weeks.
Upgrade50% OFF
Coding interviews stressing you out? Get the structure you need to succeed. Get Interview Ready In 6 Weeks.
Flowchart
Coding interviews stressing you out? Get the structure you need to succeed. Get Interview Ready In 6 Weeks.
Start Here
Coding interviews stressing you out? Get the structure you need to succeed. Get Interview Ready In 6 Weeks.
19:07
Google Engineer Explains - LeetCode #8 - String to Integer ...
16:59
String to Integer atoi 🔥| Leetcode 8 | String - YouTube
Solve String to Integer (atoi) in Python | LeetCode #8, Step- ...
- YouTube
17:11
LeetCode in Python 8. String to Integer (atoi) - Michelle小梦想家 ...
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
LeetCode
leetcode.com › problems › string-to-integer-atoi › solutions › 3232205 › python-solution-for-string-to-integer-atoi-problem
Python solution for String to Integer (atoi) problem
February 26, 2023 - 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 ...
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
Hey LeetCode enthusiasts 👋! It’s time to look at yet another problem from LeetCode String To Integer (atoi) Problem Statement Implement which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace…
Codesays
codesays.com › 2014 › solution-to-string-to-integer-atoi-by-leetcode
Solution to String to Integer (atoi) by LeetCode – Code Says
March 27, 2014 - C code. C code run. Run code run… please · Question: http://oj.leetcode.com/problems/string-to-integer-atoi/
LeetCode
leetcode.com › problems › add-strings › discuss › 528459 › Python-Solution-by-Converting-string-to-integer
Python Solution by Converting string to integer - Add Strings
March 4, 2020 - 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 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.
YouTube
youtube.com › watch
LeetCode 8: "String" to Integer — Python Solution in 3 Minutes #pythonprogramming #python #coding - YouTube
In this video, I explain how to solve the String to Integer (atoi) | LeetCode Problem #8 in Python 3.What you will learn:How to handle leading whitespaceDete...
Published: September 23, 2025
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
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
YouTube
youtube.com › watch
Google Engineer Explains - LeetCode #8 - String to Integer (atoi) - Solution (Python) - YouTube
Explanation of LeetCode #8 - String to Integer (atoi) with discussion of Time and Space Complexity and Integer Overflow.(Medium Difficulty)Notes and Code - h...
Published: May 27, 2020