Go solution [6ms]


  • 0
    func reverse(x int) int {
        
        s := 1
        limit := 0x7fffffff
        if x < 0 {
            s = -1
            x = -x
            limit = 0x80000000
        }
        
        r := 0
        for x > 0 {
            r = r * 10 + x % 10
            if r > limit {
                return 0
            }
            x /= 10
        }
        
        return s * r
    }
    

Log in to reply
 

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