golang 6ms solution for overflow


  • 0
    func reverse(x int) int {
        r := 0
        MAX := 1<<31-1
        MIN := -MAX-1
        for ; x != 0 ; {
                remainder := x%10
                new := r*10 + remainder
                if new > MAX || new < MIN  {
                    return 0
                }
                r = new
                x = x/10
        }
        return r
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.