Solution in Swift


  • 0
    N
    class Solution {
        func reverse(_ x: Int) -> Int {
            var r = String()
            let str = String(abs(x))
            for character in str.characters {
                r = "\(character)" + r
            }
            if (x < 0) {
                r = "-" + r
            }
    
            let result = Int(r)!
            if (abs(result) > Int(Int32.max)) {
                return 0;
            }
            return result
        }
    }
    

Log in to reply
 

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