Solution in Swift


  • 0
    V

    class Solution {
    func reverse(_ x: Int) -> Int {

    var result:Int32 = 0
    var temp = abs(Int32(x))
    
    while temp != 0 {
        let _result = result
        result = result &* 10 &+ temp % 10
        //check: overflow exists
        if (result - result % 10) / 10 != _result {
            return 0
        }
        temp = (temp - temp % 10) / 10
    }
    
    return x < 0 ? Int(-result) : Int(result)
    

    }
    }


Log in to reply
 

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