-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_integer_0007.py
More file actions
29 lines (29 loc) · 982 Bytes
/
reverse_integer_0007.py
File metadata and controls
29 lines (29 loc) · 982 Bytes
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
class Solution:
def reverse(self, x: int) -> int:
"""
Reverse digits of an integer.
Example 1:
Input: x = 123
Output: 321
Example 2:
Input: x = -123
Output: -321
Example 3:
Input: x = 120
Output: 21
Note:
Assume we are dealing with an environment which could only store
integers within the 32-bit signed integer range: [−2^31, 2^31 − 1].
For the purpose of this problem, assume that
your function returns 0 when the reversed integer overflows.
"""
int_min, int_max, reversed_num = -2**31, 2**31 - 1, 0
sign = 1 if x >= 0 else -1
x_abs = abs(x)
while x_abs:
digit = x_abs % 10
reversed_num = reversed_num * 10 + digit
if sign * reversed_num < int_min or sign * reversed_num > int_max:
return 0
x_abs //= 10
return sign * reversed_num