class Solution:
def myAtoi(self, s: str) -> int:
flag = 1
s = s.strip(' ')
if len(s) == 0: return 0
if s[0] in '-+':
if s[0] == '-': flag = -1
s = s[1:]
if len(s) == 0:
return 0
num = ''
for ch in s:
if '0' <= ch <= '9':
num = num + ch
else:
break
if len(num) < 1 or num == 0:
return 0
return max(min(int(num) * flag, 2 ** 31 - 1), -2 ** 31)