Skip to content

Instantly share code, notes, and snippets.

@BaReinhard
Created February 21, 2019 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BaReinhard/dd92a7456a012acbb761eeb2a3cdbfc3 to your computer and use it in GitHub Desktop.
Save BaReinhard/dd92a7456a012acbb761eeb2a3cdbfc3 to your computer and use it in GitHub Desktop.
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