Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions python/7-Reverse-Integer.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
class Solution:
def reverse(self, x: int) -> int:
# Integer.MAX_VALUE = 2147483647 (end with 7)
# Integer.MIN_VALUE = -2147483648 (end with -8 )

MIN = -2147483648 # -2^31,
MAX = 2147483647 # 2^31 - 1

res = 0
while x:
digit = int(math.fmod(x, 10)) # (python dumb) -1 % 10 = 9
x = int(x / 10) # (python dumb) -1 // 10 = -1

if res > MAX // 10 or (res == MAX // 10 and digit >= MAX % 10):
return 0
if res < MIN // 10 or (res == MIN // 10 and digit <= MIN % 10):
return 0
res = (res * 10) + digit

return res
class Solution:
def reverse(self, x: int) -> int:
# Integer.MAX_VALUE = 2147483647 (end with 7)
# Integer.MIN_VALUE = -2147483648 (end with -8 )
MIN = -2147483648 # -2^31,
MAX = 2147483647 # 2^31 - 1
res = 0
while x:
digit = int(math.fmod(x, 10)) # (python dumb) -1 % 10 = 9
x = int(x / 10) # (python dumb) -1 // 10 = -1
if res > MAX // 10 or (res == MAX // 10 and digit >= MAX % 10):
return 0
if res < MIN // 10 or (res == MIN // 10 and digit <= MIN % 10):
return 0
res = (res * 10) + digit
return res