-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_integer.py
32 lines (27 loc) · 1.01 KB
/
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
# https://leetcode.com/problems/reverse-integer/description/
# git add . && git commit -m "completed reverse_integer" && git push && exit
class Solution:
def reverse(self, x: int) -> int:
is_neg = x < 0
check_list = ['2','1','4','7','4','8','3','6','4','8' if is_neg else '7']
if is_neg:
x_list = list(str(x)[1:])
else:
x_list = list(str(x))
start = 0
end = len(x_list) - 1
while start < end:
x_list[start], x_list[end] = x_list[end], x_list[start]
start += 1
end -= 1
if len(x_list) == len(check_list):
in_range = False
for index in range(len(x_list)):
if x_list[index] < check_list[index]:
in_range = True
break
elif x_list[index] > check_list[index]:
break
if not in_range:
return 0
return int("".join(x_list)) * (-1 if is_neg else 1)