-
Notifications
You must be signed in to change notification settings - Fork 2
/
__init__.py
44 lines (35 loc) · 945 Bytes
/
__init__.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
# --- Directions
# Given an integer, return an integer that is the reverse
# ordering of numbers.
# --- Examples
# reverseInt(15) === 51
# reverseInt(981) === 189
# reverseInt(500) === 5
# reverseInt(-15) === -51
# reverseInt(-90) === -9
# # Method 1
# def reverseInt(n: int) -> int:
# # type conversion solution
# negative = False
# if n < 0:
# negative = True
# n = n*-1
# reversed = ''
# for c in str(n):
# reversed = c + reversed
# reversed = int(reversed)
# reversed = reversed*-1 if negative else reversed
# return reversed
# Method 2
def reverseInt(n: int) -> int:
# doesn't use type conversion
result = 0
negative = True if n < 0 else False
n = n*-1 if negative else n
while n >= 10:
result += (n%10)
result = result*10
n //= 10
result += n
result = result*-1 if negative else result
return result