see Code
Complexity T : O(n) M : O(n)
class Solution:
def myAtoi(self, s):
"""
:type str: str
:rtype: int
"""
import re
INT_MAX = 2**31 - 1
INT_MIN = -2**31
res = 0
pattern = r'( *[+-]?[0-9]+)'
if re.match(pattern, s):
res = int(re.match(pattern, s).group())
if res > INT_MAX:
return INT_MAX
if res < INT_MIN:
return INT_MIN
return res