We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 9e01c19 commit c8b8724Copy full SHA for c8b8724
7-Reverse-Integer.py
@@ -1 +1,22 @@
1
+class Solution:
2
+ def reverse(self, x: int) -> int:
3
+ # Integer.MAX_VALUE = 2147483647 (end with 7)
4
+ # Integer.MIN_VALUE = -2147483648 (end with -8 )
5
6
+ MIN = -2147483648 # -2^31,
7
+ MAX = 2147483647 # 2^31 - 1
8
+
9
+ res = 0
10
+ while x:
11
+ digit = int(math.fmod(x, 10)) # (python dumb) -1 % 10 = 9
12
+ x = int(x / 10) # (python dumb) -1 // 10 = -1
13
14
+ if (res > MAX // 10 or
15
+ (res == MAX // 10 and digit >= MAX % 10)):
16
+ return 0
17
+ if (res < MIN // 10 or
18
+ (res == MIN // 10 and digit <= MIN % 10)):
19
20
+ res = (res * 10) + digit
21
22
+ return res
0 commit comments