Skip to content

Instantly share code, notes, and snippets.

@leegould
Created November 23, 2016 20:45
Show Gist options
  • Save leegould/bc408c7f6ba0205c1556839fa0ab57a0 to your computer and use it in GitHub Desktop.
Save leegould/bc408c7f6ba0205c1556839fa0ab57a0 to your computer and use it in GitHub Desktop.
Reverse an int
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