leetcode 字符串转换整数 (atoi)

class Solution(object):
    def myAtoi(self, x):
        """
        :type x: int
        :rtype: int
        """
        pp=1
        xs=list(str(x))
        
        xt=[]
        
        all_number=[str(p) for p in list(range(10)) ]
        
        pp=1
        for k in xs:
            if k  not in all_number+['-',' ']:
                print(k)
                return 0
            elif k =='-':
                pp=-1
            elif k in all_number:
                xt.append(k)
            else:
                continue
                print(xt)

        print(xs)
        n=len(xt)
        print(n)
        x1=0

        for p in range(n):

            x1=x1+int(xt[p])*10**(n-p-1)
        return x1*pp
    
c=Solution()

a='    -45'
c1=c.myAtoi(a)


print('c1=',c1)
    
posted @ 2022-08-19 22:49  luoganttcc  阅读(15)  评论(0)    收藏  举报