美文网首页
8. 字符串转换整数 (atoi)

8. 字符串转换整数 (atoi)

作者: 潇湘demi | 来源:发表于2021-12-15 10:47 被阅读0次

 def myAtoi(self, s):

        """

        :type s: str

        :rtype: int

        """

        s = s.strip()

        if not s:

            return 0 

        s1=[]

        ss = ""

        fushu = False

        if s[0] == "+" or s[0]=="-" :

            ss = s[1:]

            if s[0]=="-":

                fushu = True

        else:

            ss = s

        for s in ss:

            if s>= "0" and s<= "9":

                s1.append(s)

            else:

                break

        print(s1)

        if len(s1) == 0:

            return 0

        return min(int("".join(s1)),2**31-1)if fushu==False else max(-int("".join(s1)),-2**31)

相关文章

网友评论

      本文标题:8. 字符串转换整数 (atoi)

      本文链接:https://www.haomeiwen.com/subject/elnufrtx.html