From ddb56fbd22e9fab0930cfbad71ce6c56dbcf18f1 Mon Sep 17 00:00:00 2001 From: Prince Date: Sat, 15 Oct 2022 21:16:18 +0530 Subject: [PATCH] Add files via upload --- python/7-Reverse-Integer.py | 40 ++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/python/7-Reverse-Integer.py b/python/7-Reverse-Integer.py index 01e9c13..bcc9e86 100644 --- a/python/7-Reverse-Integer.py +++ b/python/7-Reverse-Integer.py @@ -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