美文网首页
#461 Hamming Distance

#461 Hamming Distance

作者: 乐正君Krizma | 来源:发表于2017-04-27 21:35 被阅读0次

题目:

The Hamming Distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.



代码:

/**

* @param {number} x

* @param {number} y

* @return {number}

*/

var hammingDistance = function(x, y) {

var n = 0, s = x ^ y;

for(n = 0; n < s; n++){

s &= (s - 1)

}

return n;

};

相关文章

网友评论

      本文标题:#461 Hamming Distance

      本文链接:https://www.haomeiwen.com/subject/fpbnzttx.html