使用re正则表达式来搜索
class Solution:
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
import re
res = re.findall(r"^[\+\-]?\d+",str.strip())
#^匹配字符串的开头,[]匹配一组字符,\d匹配数字
print(res)
if res !=[]:
if int(res[0]) > (2**31-1):
return (2**31-1)
if int(res[0]) < (-2**31):
return (-2**31)
return int(res[0])
else:
return 0
彩蛋:)
关于正则式表达还有一个爬虫小项目 https://blog.youkuaiyun.com/qq_38484259/article/details/84567169 欢迎指正