-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0007_Reverse_Integer.py
39 lines (30 loc) · 1.57 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
##################
# First Solution #
##################
# class Solution:
# def reverse(self, x): # type x: int
# sign = [1, -1][x < 0] # sign = -1 if x < 0 else 1
# rev_num = sign * int(str(abs(x))[::-1]) # [start:stop:step], -1 step means reverse step
# # only sequence data types can be sliced in this way
# if -pow(2, 31) <= rev_num <= pow(2, 31) - 1: # set the range
# return rev_num
# else:
# return 0
###################
# Second Solution #
###################
class Solution:
def reverse(self, x): # type x: int
sign = [1, -1][x < 0] # sign = -1 if x < 0 else 1
rev_num = 0
num = abs(x) # abs() function returns the absolute value of a number
while num:
num, mod = divmod(num, 10) # num is quotient, mod is remainder
rev_num = (rev_num * 10) + mod # creating reverse num
if -pow(2, 31) <= rev_num <= pow(2, 31) - 1: # if -(2**31) <= rev_num <= (2**31) - 1
return sign * rev_num # if in the range, return with a sign
else:
return 0
print(Solution().reverse(123)) # 321
print(Solution().reverse(-123)) # -321
print(Solution().reverse(120)) # 21