My accepted 16 line of code for Go


  • 0
    A

    Checks both positive and negative case

    func reverse(x int) int {
        max := math.MaxInt32
        retInt := 0
        for  {
            // return 0 if overflow
            if retInt + x%10 > max || -(retInt + x%10) > max{
                return 0
            }
            retInt += x%10
            x /= 10
            if x == 0 {
                break
            }
            retInt *= 10
        }
        return retInt
    }
    

Log in to reply
 

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