LeetCode 461 Hamming Distance(汉明距离)
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 461 Hamming Distance(汉明距离)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Q: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.
注:兩個等長字符串之間的漢明距離(英語:Hamming distance)是兩個字符串對應位置的不同字符的個數。換句話說,它就是將一個字符串變換成另外一個字符串所需要替換的字符個數。
Note:
0 ≤ x, y < 231.
Example:
Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
The above arrows point to positions where the corresponding bits are different
S:
public class Solution {
}
bitCount 自實現方案:
int d = 0;int bitxor = x ^ y;while (bitxor > 0){if (bitxor % 2 == 1){d++;}bitxor = bitxor >> 1;}return d;總結
以上是生活随笔為你收集整理的LeetCode 461 Hamming Distance(汉明距离)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 国产影像新旗舰曝光:50Mp 1/1.1
- 下一篇: JDBC(二)驱动程序类型发展历程