class Solution:
def myAtoi(self, str: str) -> int:
str = str.strip()
if len(str) == 0:
return 0
elif str[0] != '-' and str[0] != '+' and not str[0].isdigit():
return 0
ans = 0
start, end = 0, 0
isNegative = False
if str[0] == '+':
start += 1
end += 1
elif str[0] == '-':
isNegative = True
start += 1
end += 1
if start >= len(str) or not str[start].isdigit():
return 0
while end < len(str) and str[end].isdigit():
end += 1
ans = int(str[start:end])
if isNegative:
ans = -ans
if ans < -2**31:
ans = -2**31
elif ans > 2**31 - 1:
ans = 2**31 - 1
return ans