-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreverse_integer.py
50 lines (40 loc) · 1.42 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution:
def reverse(self, x: int) -> int:
# handle the sign (before reverse)
sign = 0
if x < 0:
sign = -1
x = -x # note: we keep the digits
else:
sign = 1
# by using 'str' in Python
my_str = str(x)
# reverse string
my_str = my_str[::-1]
# using lstrip() to remove '0' on the 'left' side
my_str = my_str.lstrip('0')
###############################
# test_str = '12000'
# test_str = test_str[::-1]
# test_str = test_str.lstrip('0')
# print(test_str)
###############################
# be careful (after using strip)
# special case (e.g., '0000000')
if my_str == '':
return 0
# handle the sign (after reverse)
if sign == -1:
my_str = '-' + my_str
# by using 'int' in Python
my_integer = int(my_str)
# handle the 'overflow/underflow' problems
# note: the power operator in Python is **
max_int = (2 ** 31) -1
print(max_int)
min_int = -(max_int +1)
print(min_int)
# handle the 'overflow/underflow' cases
if (my_integer < min_int) or (my_integer > max_int):
return 0
return my_integer