-
Notifications
You must be signed in to change notification settings - Fork 0
/
#0007 Reverse Integer.py
39 lines (30 loc) · 1.18 KB
/
#0007 Reverse Integer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution:
def reverse(self, x: int) -> int:
"""
Reverses the digits of an integer, returning 0 if the result overflows a 32-bit signed integer.
Args:
x (int): The integer to reverse.
Returns:
int: The reversed integer or 0 if it overflows.
"""
# Define the 32-bit signed integer bounds
upper_limit = 2**31
lower_limit = - upper_limit
result = 0
# Determine the sign of the number and work with its absolute value
sign = 1 if x >= 0 else -1
x = abs(x)
while x != 0:
digit = x % 10
x //= 10
# Check if the result will overflow if we add the new digit
if result > (upper_limit - digit) // 10:
return 0
# Update result with the new digit
result = result * 10 + digit
# Restore the original sign
result *= sign
# Check if the reversed result is within the 32-bit signed integer range
if result > upper_limit or result < lower_limit:
return 0
return result