0008字符串转换整数

字符串转换整数
描述:
0008字符串转换整数_第1张图片

class Solution(object):
    def myAtoi(self, s):
        """
        :type s: str
        :rtype: int
        """
        T = True  # 判断正负
        num = 0
        i = 0
        if len(s)==0:
            return num
        while s[i] == ' ':
            i += 1
            if i == len(s):
                return num
        if s[i] == '-':
            T = False
            i+=1
        elif s[i] == '+':
            i+=1
        if i == len(s):
            return num
        while '0' <= s[i] <= '9':
            num *= 10
            num += int(s[i])
            i += 1
            if i == len(s):
                break
        if not T:
            num = -num
        num = max(-2 ** 31, num)
        num = min(2 ** 31 - 1, num)
        return num

你可能感兴趣的:(leetcode,leetcode,python)