class Solution:
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
if str=="":
return 0
f = 0
for i in range(len(str)):
s=str[i]
if s ==" ":
continue
elif s=="-":
if i== len(str)-1 or not (str[i+1]>='0' and str[i+1]<='9'):
return 0
if f==0:
f = -1
else:
return 0
elif s=="+":
if i== len(str)-1 or not (str[i+1]>='0' and str[i+1]<='9'):
return 0
if f==0:
f=1
else:
return 0
elif s>='0' and s<='9':
break
else:
return 0
res=''
while i<len(str) and str[i]>='0' and str[i]<='9':
res+=str[i]
i+=1
if res=="":
return 0
re = int(res)
if f==-1:
re=-re
if re>2**31-1:
return 2**31-1
if re<-2**31:
return -2**31
return re