forked from HuberTRoy/leetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseInteger.py
47 lines (34 loc) · 976 Bytes
/
ReverseInteger.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
"""
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.
翻转一个整数。
我直接用了转成字符串,然后取反然后转成整数的方法。
效率过得去。
beat 45%.
测试地址:
https://leetcode.com/problems/reverse-integer/description/
"""
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
# str_x = str(x)
if x < 0:
x = -int(str(abs(x))[::-1])
else:
x = int(str(abs(x))[::-1])
if x > 2**31 or x < -2**31:
return 0
return x