Skip to content

Instantly share code, notes, and snippets.

@viveksyngh
Created August 5, 2015 19:34
Show Gist options
  • Select an option

  • Save viveksyngh/5b90b64636f8326b323b to your computer and use it in GitHub Desktop.

Select an option

Save viveksyngh/5b90b64636f8326b323b to your computer and use it in GitHub Desktop.
Reverse an Integer
#Reverse digits of an integer.
#Return 0 if the result overflows and does not fit in a 32 bit signed integer
def reverse(self, A):
rev = 0
flag = False
if A < 0 :
flag = True
A = abs(A)
while A != 0 :
rev = rev * 10 + (A%10)
A = A/10
if rev > 2**31 :
return 0
if flag :
return -rev
else :
return rev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment