LeetCode每日一题——T8. 字符串转换整数 (atoi) (中):正则表达式

class Solution:
    def myAtoi(self, str: str) -> int:        
        return max(min(int(*re.findall('^[\+\-]?\d+', str.lstrip())), 2**31 - 1), -2**31)

max(min(a, b), c):设定上下限

*:解包:findall 函数返回所有找到的结果,用一个 list 表示,*是解包
^:匹配字符串开头
[+-]:代表一个+字符或-字符
?:前面一个字符可有可无
\d:一个数字
+:前面一个字符的一个或多个

str.lstrip([chars]):去除字符串左侧一串 “chars” 元素
str.strip([chars]):去除字符串中所有 “chars” 元素