-
Notifications
You must be signed in to change notification settings - Fork 43
/
reverse-integer.py
145 lines (123 loc) · 3.24 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""
7. Reverse Integer
Medium
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
Example 1:
Input: x = 123
Output: 321
Example 2:
Input: x = -123
Output: -321
Example 3:
Input: x = 120
Output: 21
Constraints:
-231 <= x <= 231 - 1
"""
# V0
# IDEA : BRUTE FORCE
class Solution(object):
def reverse(self, x):
# edge case
if x == 0:
return 0
minus = False
x = str(x)
print ("x = " + str(x))
if x[0] == "-":
minus = True
x = x[1:]
x_ = int(x[::-1])
if not minus:
return x_ if x_ < 2**31 - 1 else 0
else:
x_ = -1 * x_
return x_ if x_ > (-1) * 2**31 else 0
# V0
class Solution(object):
def reverse(self, x):
if x < 0:
x = -int(str(x)[1:][::-1])
else:
x = int(str(x)[::-1])
x = 0 if abs(x) > 0x7FFFFFFF else x
return x
# test case
# s = Solution()
# assert s.reverse(0) == 0
# assert s.reverse(10) == 1
# assert s.reverse(1000) == 1
# assert s.reverse(-1) == -1
# assert s.reverse(-10) == -1
# assert s.reverse(-1) == -1
# assert s.reverse(0x7FFFFFFF) == 0
# assert s.reverse(-0x7FFFFFFF) == 0
# to check
#assert s.reverse(0x7FFFFFFF - 1) == 0
# V1
class Solution(object):
# x = 20010000
# x_reverse = int(str(x)[::-1])
# x_reverse = 1002
def reverse(self, x):
if x < 0:
x = -int(str(x)[1:][::-1])
else:
x = int(str(x)[::-1])
x = 0 if abs(x) > 0x7FFFFFFF else x
return x
# V1'
# https://blog.csdn.net/coder_orz/article/details/52039990
class Solution(object):
def reverse(self, x):
flag = 1 if x >= 0 else -1
new_x, x = 0, abs(x)
while x:
new_x = 10 * new_x + x % 10
x /= 10
new_x = flag * new_x
return new_x if new_x < 2147483648 and new_x >= -2147483648 else 0
# V1''
# https://blog.csdn.net/coder_orz/article/details/52039990
class Solution(object):
def reverse(self, x):
x = int(str(x)[::-1]) if x >= 0 else - int(str(-x)[::-1])
return x if x < 2147483648 and x >= -2147483648 else 0
# V2
# Time: O(logn) = O(1)
# Space: O(1)
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
if x < 0:
return -self.reverse(-x)
result = 0
while x:
result = result * 10 + x % 10
x //= 10
return result if result <= 0x7fffffff else 0 # Handle overflow.
def reverse2(self, x):
"""
:type x: int
:rtype: int
"""
if x < 0:
x = int(str(x)[::-1][-1] + str(x)[::-1][:-1])
else:
x = int(str(x)[::-1])
x = 0 if abs(x) > 0x7FFFFFFF else x
return x
def reverse3(self, x):
"""
:type x: int
:rtype: int
"""
def cmp(a, b):
return (a > b) - (a < b)
s = cmp(x, 0)
r = int(repr(s * x)[::-1])
return s * r * (r < 2 ** 31)