Sunday, September 7, 2014

Leetcode: Reverse Integer @Python

Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321

In C++, -5%7=-5; in Python -5%7=2.
class Solution:
    # @return an integer
    def reverse(self, x):
        y=0
        digit=0
        if x<0:
            checker =-1
            x=x*(-1)
        else:
            checker=1
        while(x>0):
            lastdigit=x%10
            y=y*10+lastdigit
            x=int(x/10)
        return int(y*checker)

No comments :

Post a Comment