Swift solution


  • 0
    S

    class Solution {
    func hammingDistance(_ x: Int, _ y: Int) -> Int {

       let k:Int = x^y
        
        let s:String = String(k, radix: 2)
        
        var ret:Int = 0
        for m:Int in 0...s.characters.count - 1 {
        
            let index = s.characters.index(s.characters.startIndex, offsetBy: m)
            let c:Character = s.characters[index]
            
            if(c=="1") {
            ret = ret + 1
            }
        }
        
        return ret
    
    }
    

    }


Log in to reply
 

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