We process the string step-by-step: trimming, checking sign, and then accumulating digits while checking for overflow.
s = s.lstrip(). If empty, return 0.- Check first character for
+or-. Setsign. - Iterate through characters:
- If character is a digit, add to
res. - If not a digit, stop.
- If character is a digit, add to
- Apply
sign. - Clamp result to
[-2^31, 2^31 - 1].
- Time Complexity: O(N).
- Space Complexity: O(1).
def my_atoi(s):
s = s.strip()
if not s: return 0
sign = 1
i = 0
if s[0] == '-':
sign = -1
i = 1
elif s[0] == '+':
i = 1
res = 0
while i < len(s) and s[i].isdigit():
res = res * 10 + int(s[i])
i += 1
res *= sign
# Clamping
INT_MAX = 2**31 - 1
INT_MIN = -2**31
if res > INT_MAX: return INT_MAX
if res < INT_MIN: return INT_MIN
return res