Leetcode Medium 2 String to Integer (atoi)

class Solution:
    def myAtoi(self, str):
        s = ''
        str = str.lstrip()
        for i in range(len(str)):
            if str[i].isdigit() or ((str[i] == '+' or str[i] == '-') and i == 0):
                s += str[i] 
            else:
                break

        if s == '' or s == '+' or s == '-':
            return 0

        s = int(s)
        if s > 2**31 - 1:
            return 2**31 - 1
        elif s < -2**31:
            return -2**31
        return s

        
        

猜你喜欢

转载自blog.csdn.net/li_k_y/article/details/86383414
今日推荐