python
class Solution:
def myAtoi(self, s: str):
s = s.strip()
if not s:
return 0
elif not s[0].isdigit():
if s[0] == '+' or s[0] == '-':
if len(s) == 1 or not s[1].isdigit():
return 0
else:
return 0
def search_num(arr):
if not arr:
return None
start, end = 0, 0
n = len(arr)
while end < n and not arr[end].isdigit():
end += 1
if end >= n:
return None
start = end
while end < n and arr[end].isdigit():
end += 1
if start >=1 and arr[start-1] == '-':
num = -int(arr[start:end])
if num < -pow(2, 31):
return -pow(2, 31)
else:
return num
else:
num = int(arr[start:end])
if num > pow(2, 31) - 1:
return pow(2, 31)-1
else:
return num
res = search_num(s)
if res is not None:
return res