Skip to content

Commit

Permalink
GO: 7. Reverse Integer
Browse files Browse the repository at this point in the history
  • Loading branch information
MaratKhakim committed Aug 18, 2022
1 parent 191c61c commit f68fdb7
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions go/7-Reverse-Integer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
func reverse(x int) int {
negative := x < 0
num := 0

if negative {
x = -x
}

for x > 0 {
if math.MaxInt32/10 < num {
return 0
}

num = 10*num + x%10
x /= 10
}

if negative {
return -num
}

return num
}

0 comments on commit f68fdb7

Please sign in to comment.