题目

解析:注意的点:正负需前导才生效

class Solution:
    def myAtoi(self, s: str) -> int:
        s = s.strip()
        flag = 0
        nums = ""
        for c in s:
            if c == "+" and flag == 0 and len(nums) == 0:
                flag = 1
                continue
            if c == "-" and flag == 0 and len(nums) == 0:
                flag = -1
                continue
            if "0" <= c <= "9":
                nums += c
                continue
            break
        if nums == "":
            return 0
        r = int(nums) * (1 if flag in [0, 1] else -1)
        if r < -2147483648:
            return -2147483648
        elif r > 2147483647:
            return 2147483647
        else:
            return r