Share my Solution with Python


  • 0
    Z
    class Solution(object):
        def reverse(self, x):
            lowerlimit = '2147483648'
            upperlimit = '2147483647'
            final = []
            flag = False
            s = str(x)
            if s[0] == '-':
                flag = True
                s = s[1:len(s)]
            for i in range(len(s)-1,-1,-1):
                final.append(s[i])
            final = ''.join(final)
            if flag:
                if len(final) == 10:
                    for i in range(10):
                        if cmp(final[i],lowerlimit[i]) > 0:
                            return 0
                        elif cmp(final[i], lowerlimit[i]) < 0:
                            break
                return -int(final)
            else:
                if len(final) == 10:
                    for i in range(10):
                        if cmp(final[i],upperlimit[i]) > 0:
                            return 0
                        elif cmp(final[i], upperlimit[i]) < 0:
                            break
                return int(final)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.