MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / reverse

Function reverse

reverse_integer_7/solution.go:5–18  ·  view source on GitHub ↗
(x int)

Source from the content-addressed store, hash-verified

3import "math"
4
5func reverse(x int) int {
6 var out int64
7 for x != 0 {
8 out = (out * 10) + int64(x%10)
9 x = x / 10
10 }
11
12 // if out overflows positive (2^32 - 1) or negative (-2^31) 32bit integer
13 if out > math.MaxInt32 || out < math.MinInt32 {
14 return 0
15 }
16
17 return int(out)
18}

Callers 1

Test_reverseFunction · 0.70

Calls

no outgoing calls

Tested by 1

Test_reverseFunction · 0.56