Created
July 26, 2018 12:18
-
-
Save waynejo/5f74938d4d6a69a7d750746a9917826e 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 { | |
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