-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathreverse_integer.py
65 lines (55 loc) · 1.61 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Given a 32-bit signed integer, reverse digits of an integer.
# Example 1:
# Input: 123
# Output: 321
# Example 2:
# Input: -123
# Output: -321
# Example 3:
# Input: 120
# Output: 21
# Note:
# Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
# A very bad solution
class Solution:
def reverse(self, x: int) -> int:
if x >= 2**31-1 or x <= -2**31: return 0
str_int = str(x)
new_str = []
if str_int[0] == '-':
new_str.append(str_int[0])
start = 1
else:
start = 0
i = len(str_int) -1
str1 = ""
while (i >= start):
new_str.append(str_int[i])
i = i-1
reverse = int(str1.join(new_str))
if reverse >= 2**31-1 or reverse <= -2**31: return 0
return int(str1.join(new_str))
# Solution 2
# Time Complexity: O(log(x))
# Space Complexity O(1)
INT_MAX = 2**31-1
INT_MIN = -2**31
class Solution:
def reverse(self, x: int) -> int:
if x >= INT_MAX or x <= INT_MIN: return 0
if x == 0: return x
if x < 0:
mult = -1
x = x*(-1)
else:
mult = 1
rev = 0
num = x
while(num != 0 ):
dig = num % 10
num = num // 10
if rev > INT_MAX/10:
return 0
else:
rev = rev*10 + dig
return rev*mult