[LeetCode][python3]0007. Reverse Integer

Start the Journey
N2I -2020.03.15

1. My first solution


class Solution:
    def reverse(self, x: int) -> int:
        sign=1
        if x<0:
            sign=-1
            x=-x
        s=str(x)
        s=s[::-1]
        x=int(s)*sign
        if x > 2147483648-1 or x<-2147483648:
            return 0
        return x


Explanation:

In Python 3, the plain int type is unbounded. So we don’t have to deal with the signed integer range. The 2147483648-1 and -2147483648 repersent[-2^31,2^31-1].



Comments