Hamming Distance Swift


  • 0
    S
    
    class Solution {
        func hammingDistance( _ x: Int, _ y: Int) -> Int {
            var strX = String(x, radix: 2)
            var strY = String(y, radix: 2)
            if strX.characters.count > strY.characters.count {
                let padd = String(repeatElement(Character("0"), count: (strX.characters.count - strY.characters.count)))
                strY = padd + strY
            } else if strY.characters.count > strX.characters.count {
                let padd = String(repeatElement(Character("0"), count: strY.characters.count - strX.characters.count))
                strX = padd + strX
            }
            let charY = Array(strY.characters)
            let charX = Array(strX.characters)
            print(charX)
            print(charY)
            var diffCount = 0
                for i in 0..<charY.count {
                    if charX[i] != charY[i] {
                         diffCount = diffCount + 1
                    }
                }
            print(diffCount)
            return diffCount
        }
    }
    
    Solution().hammingDistance(1,4)
    
    

Log in to reply
 

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