LeetCode-461. Hamming Distance

网友投稿 820 2022-08-25

LeetCode-461. Hamming Distance

LeetCode-461. Hamming Distance

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.

Note: 0 ≤ ​​​x​​​, ​​y​​ < 231.

Example:

Input: x = 1, y = 4Output: 2Explanation:1 (0 0 0 1)4 (0 1 0 0) ↑ ↑The above arrows point to positions where the corresponding bits are different.

题解:

class Solution {public: int hammingDistance(int x, int y) { int ans = 0, n = x ^ y; while (n > 0) { ans += n % 2; n /= 2; } return ans; }};

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:LeetCode-905. Sort Array By Parity
下一篇:LeetCode-942. DI String Match
相关文章

 发表评论

暂时没有评论,来抢沙发吧~