leetcode-easy-string-7 Reverse Integer

mycode 

class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        a = str(x)
        if a[0] == '-':
            flag = '-'
            a = a[1:] 
        else :
            flag = ''
        a = a[::-1]
        while True:
            if a[0] == '0':
                if len(a) > 1: a = a[1:]
                else: return 0
            else:
                a = int(flag+a)
                if a > 2147483647 :
                    return 0
                elif a < -2147483648 : 
                    return 0
                else:
                    return a
                
                

NOTE: When using int (x), will automatically remove the front of the x 0

class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        a = str(x)
        if a[0] == '-':
            flag = '-'
            a = a[1:] 
        else :
            flag = ''
        a = a[::-1]
        #while True:
        #    if a[0] == '0':
        #        if len(a) > 1: a = a[1:]
        #        else: return 0
        #    else:
        a = int(flag+a)
        if a > 2147483647 :
            return 0
        elif a < -2147483648 : 
            return 0
        else:
            return a

 

reference

class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        x = int(str(x)[::-1]) if x >= 0 else - int(str(-x)[::-1])
        return x if x < 2147483648 and x >= -2147483648 else 0

  

Guess you like

Origin www.cnblogs.com/rosyYY/p/10993950.html