Ruby One line Solution


  • 0
    B

    XOR gets produces 1 when the two bits are different i.e (1,0) and (0,1),
    And XOR in Ruby is '^',
    To get the result as string, use the formatter '%b',
    And then count the ones in the resulted string,
    That's your hamming distance!

    ("%b" % (x^y)).count('1')
    

Log in to reply
 

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