Converts a string to an integer, mimicking the behavior of the C atoi function.
Explanation:
- Whitespace Removal:
- The input string
sis stripped of leading and trailing whitespace usings.strip().
- The input string
- Empty String Check:
- If the stripped string is empty, the function returns 0.
- Sign Determination:
- The code checks the first character of the string for a sign (
'+'or'-'). - If a
'-'is found, thesignvariable is set to -1; otherwise, it defaults to 1. - The
indexis incremented to move past the sign character.
- The code checks the first character of the string for a sign (
- Digit Extraction and Conversion:
- The code iterates through the remaining characters of the string as long as they are digits.
- For each digit, it converts the character to its integer equivalent using
int(s[index])and adds it to thevalvariable. - The
valvariable is multiplied by 10 in each iteration to construct the integer.
- Overflow Handling:
- During the integer construction, the code checks if
valexceeds the 32-bit signed integer range ([-2147483648, 2147483647]). - If an overflow occurs:
- If the
signis positive, it returns2147483647. - If the
signis negative, it returns-2147483648.
- If the
- During the integer construction, the code checks if
- Return Value:
- Finally, the function returns the calculated integer value (
sign * val).
- Finally, the function returns the calculated integer value (
class Solution:
def myAtoi(self, s: str) -> int:
s = s.strip()
if not s:
return 0
sign = 1
index = 0
val = 0
if s[0] == '-':
sign = -1
index += 1
elif s[0] == '+':
index += 1
while index < len(s) and s[index].isdigit():
val = val * 10 + int(s[index])
if val > 2147483647 or val < -2147483648:
if sign == 1:
return 2147483647
else:
return -2147483648
index += 1
return sign * valfunc myAtoi(s string) int {
if len(s) == 0 {
return 0
}
sign := 1 // -1 negative
index := 0
val := 0
for len(s) > index && s[index] == ' ' {
index++
}
if index >= len(s) {
return 0
}
if v := s[index]; v == '-' || v == '+' {
if v == '-' {
sign = -1
}
index++
}
for {
if index == len(s) {
break
}
if isNumericRune(s[index]) == false {
return sign * val
}
val = val*10 + int(s[index]) - '0'
if val > 2147483647 || val < -2147483648 {
if sign == 1 {
return 2147483647
}
return -2147483648
}
index++
}
return sign * val
}
func isNumericRune(x byte) bool {
return x >= '0' && x <= '9'
}Time Complexity:
- O(n), where
nis the length of the input strings.- The
strip()operation takes O(n) time in the worst case. - The
whileloop iterates through the string at most once.
- The
Space Complexity:
- O(1) (constant).
- The code uses a fixed number of variables (
sign,index,val), regardless of the input string's size. - The
strip()operation in python generally returns a new string, but in the case of no leading or trailing white space, it will return the same string. Therefore the space complexity remains constant.
- The code uses a fixed number of variables (