Swift Solution 29ms


  • 0
    S
        func hammingDistance(_ x: Int, _ y: Int) -> Int {
            let difference = x ^ y
        
           // Convert to string and count the "1"s.
            return String(difference, radix: 2).characters.filter{ $0 == "1"}.count
        }
    

  • 0
    J

    why my solution is 39ms?

    class Solution {
        func hammingDistance(_ x: Int, _ y: Int) -> Int {
            return String(x ^ y , radix: 2).characters.filter{$0 == "1"}.count
        }
    }
    

Log in to reply
 

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