MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / reverse

Method reverse

python/0007-reverse-integer.py:2–20  ·  view source on GitHub ↗
(self, x: int)

Source from the content-addressed store, hash-verified

1class 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 (res == MAX // 10 and digit > MAX % 10):
15 return 0
16 if res < MIN // 10 or (res == MIN // 10 and digit < MIN % 10):
17 return 0
18 res = (res * 10) + digit
19
20 return res

Callers 7

finalPricesMethod · 0.45
snakesAndLaddersMethod · 0.45
findItineraryMethod · 0.45
alienOrderMethod · 0.45
addBinaryFunction · 0.45
sortedSquaresFunction · 0.45
addToArrayFormFunction · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected