-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7_reverse_integer.py
More file actions
48 lines (45 loc) · 1.18 KB
/
7_reverse_integer.py
File metadata and controls
48 lines (45 loc) · 1.18 KB
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
# coding=utf-8
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
if x == 0 or x < -2**31 or x > 2**31-1:
return 0
while(True):
if x % 10 == 0:
x /= 10
else:
break
if x < 0:
tmp = str(x)[1:]
result = int('-' + tmp[::-1])#
else:
result = int(str(x)[::-1])
if result < -2**31 or result > 2**31-1:
return 0
else:
return result
def reverse2(self, x):
if x == 0 or x < -2**31 or x > 2**31-1:
return 0
while(True):
if x % 10 == 0:
x /= 10
else:
break
a = list(str(x))
a.reverse() # list
# a.pop # stack
# result = reduce(lambda x,y:y+x,a) # reduce
result = "".join(a)
if result[-1] == '-':
result = result[-1] + result[0:-1]
result = int(result)
if result < -2**31 or result > 2**31-1:
return 0
return result
if __name__ == '__main__':
a = Solution()
print a.reverse2(-1534230)