Topic link

The point of this problem is to judge that the value is out of bounds. The following code has been submitted for approval.

import "math"

func reverse(x int) int {
    var (
        rev int
        maxR = math.MaxInt32/10
        maxP = math.MaxInt32%10
        minR = math.MinInt32/10
        minP = math.MinInt32%10
    )
    forx ! =0 {
        pop := x%10
        if rev > maxR || (rev == maxR && pop > maxP) {
            return 0
        }
        if rev < minR || (rev == minR && pop < minP) {
            return 0
        }
        rev = rev*10 + pop
        x /= 10
    }
    return rev
}
Copy the code