Created
August 5, 2015 19:34
-
-
Save viveksyngh/5b90b64636f8326b323b to your computer and use it in GitHub Desktop.
Reverse an Integer
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
| #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