Skip to content

Instantly share code, notes, and snippets.

@waynejo
Created July 26, 2018 12:18
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 waynejo/5f74938d4d6a69a7d750746a9917826e to your computer and use it in GitHub Desktop.
Save waynejo/5f74938d4d6a69a7d750746a9917826e to your computer and use it in GitHub Desktop.
func reverse(x int) int {
isMinus := false
var remain = x
if 0 > x {
isMinus = true
remain = -x
}
var acc = 0
for 0 < remain {
acc = acc * 10 + (remain % 10)
remain /= 10
}
if isMinus {
if (1 << 31) < acc {
return 0
}
return -acc
}
if (1 << 31) - 1 < acc {
return 0
}
return acc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment