Skip to content

Commit 5f8fd8f

Browse files
authored
Update reverse-integer.py
1 parent b1d2121 commit 5f8fd8f

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

Python/reverse-integer.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,22 @@
2020
# You would then have to re-design the function (ie, add an extra parameter).
2121
#
2222

23-
class Solution:
24-
# @return an integer
23+
class Solution(object):
2524
def reverse(self, x):
26-
ans = 0
25+
"""
26+
:type x: int
27+
:rtype: int
28+
"""
29+
result = 0
2730
if x >= 0:
2831
while x:
29-
ans = ans * 10 + x % 10
32+
result = result * 10 + x % 10
3033
x /= 10
31-
return ans if ans <= 2147483647 else 0 # Handle overflow.
34+
return result if result <= 0x7fffffff else 0 # Handle overflow.
3235
else:
3336
return -self.reverse(-x)
34-
37+
38+
3539
if __name__ == "__main__":
3640
print Solution().reverse(123)
3741
print Solution().reverse(-321)

0 commit comments

Comments
 (0)