Leetcode-Python 字符串转换整数(atoi)

Leetcode-Python 字符串转换整数(atoi)

Leetcode-Python 字符串转换整数(atoi)

class Solution:
    def myAtoi(self, s: str) -> int:
        s = s.strip(' ')
        string = '0'
        if len(s) > 0:
            if s[0] == '-' or s[0].isdigit() or s[0] == '+':
                string = s[0]
                if s[0] == '+' or s[0] == '-':
                    string += '0'
                for c in range(1,len(s)):
                    if s[c].isdigit():
                        string += s[c]
                    else:
                        break
        num = int(string)
        if num < -2**31:
            return -2**31
        elif num > 2**31-1:
            return 2**31-1
        return num

github项目地址:https://github.com/JockWang/LeetCode-Python