Swift solution


  • 0
    class Solution {
        func hammingDistance(_ x: Int, _ y: Int) -> Int {
            var count = 0
            var n = x ^ y
            
            while n > 0 {
                n &= n - 1
                count += 1
            }
            
            return count
        }
    }
    

Log in to reply
 

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