[LeetCode][python3]0007. Reverse Integer

Start the Journey
N2I -2020.03.15

1. My first solution


  1. class Solution:
  2. def reverse(self, x: int) -> int:
  3. sign=1
  4. if x<0:
  5. sign=-1
  6. x=-x
  7. s=str(x)
  8. s=s[::-1]
  9. x=int(s)*sign
  10. if x > 2147483648-1 or x<-2147483648:
  11. return 0
  12. return x
  13.  

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].