Ruka
1 min readMar 17, 2020

[LeetCode][python3]0007. Reverse Integer

Start the journey
N2I -2020.03.15

  1. My first try
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
N2iI -2020.03.15

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

Ruka
Ruka

Written by Ruka

HI I’m Ruka. Now a SWE in Taiwan. Contact via email: nayzi9999@gmail.com

No responses yet