Created
February 21, 2019 19:55
-
-
Save BaReinhard/dd92a7456a012acbb761eeb2a3cdbfc3 to your computer and use it in GitHub Desktop.
This file contains 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
func reverse(x int) int { | |
// calc minimum and maximum values | |
max:= int(math.Pow(2,31)) | |
min:= int(math.Pow(-2,31)) -1 | |
tmp := x | |
reverse:= 0 | |
for{ | |
digit := tmp % 10 | |
reverse = reverse * 10 + digit | |
tmp = tmp / 10 | |
if tmp == 0{ | |
break | |
} | |
} | |
// If the integer has overflowed return 0 | |
if reverse > max || reverse < min { | |
return 0 | |
} | |
return reverse | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment