-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7_reverse.py
More file actions
32 lines (27 loc) · 756 Bytes
/
7_reverse.py
File metadata and controls
32 lines (27 loc) · 756 Bytes
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
import math
class Solution:
def reverse(self, x: int) -> int:
if x == 2147483647 or x == -2147483648:
return 0
if x > 1000000000 and (x % 10) > 2:
return 0
isNegative = True
if x > 0:
isNegative = False
else:
x = -x
ret = 0
while x > 0:
bit = x % 10
if ret != 0 or bit != 0:
ret = ret * 10 + bit
x = (x - bit) / 10
if ret > 214748364 and x > 0:
return 0
elif ret == 214748364 and x > 7:
return 0
if isNegative:
ret = -ret
return math.ceil(ret)
if __name__ == "__main__":
Solution().reverse(-2147483412)