Created
November 23, 2016 20:45
-
-
Save leegould/bc408c7f6ba0205c1556839fa0ab57a0 to your computer and use it in GitHub Desktop.
Reverse an int
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
def reverse(self, x): | |
""" | |
:type x: int | |
:rtype: int | |
""" | |
result = 0; | |
if x > 0: | |
result = int(str(x)[::-1]) | |
else: | |
result = -int(str(abs(x))[::-1]) | |
if result > 2147483647 or result < -2147483647: | |
return 0 | |
else: | |
return result | |
s = Solution() | |
print s.reverse(0) | |
print s.reverse(123) | |
print s.reverse(-123) | |
print s.reverse(1534236469) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment