7. Reverse Integer


  • 0
    P

    func reverse(_ x: Int) -> Int {
    var negative = false, res: Double = 0, x = x

    if x < 0 {
        negative = true
        x = -x
    }
    
    while x > 0 {
        res = res*10 + Double(x%10)
        x = x/10
    }
    
    if res > Double(Int32.max) ||  res < Double(Int32.min) { return 0 }
    
    return negative == true ? -Int(res) : Int(res)
    

    }


Log in to reply
 

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