LeetCode-461. Hamming Distance
2017-01-20 10:31
387 查看
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers
思路:算两个数二进制按位比较不同位的个数,先异或,再求xor的二进制1的个数。
Given two integers
xand
y, calculate the Hamming distance.
思路:算两个数二进制按位比较不同位的个数,先异或,再求xor的二进制1的个数。
public class Solution { public int hammingDistance(int x, int y) { int xor = x ^ y; int distance = 0; while (xor != 0) { distance ++; xor &= (xor-1); //清除xor最右边的1 } return distance; } }
相关文章推荐
- LeetCode--461. Hamming Distance(汉明距离)Python
- LeetCode#461. Hamming Distance
- leetcode_461. Hamming Distance 计算汉明距离,按位异或运算,计算整数的二进制表示中1的个数 java
- [LeetCode]461. Hamming Distance(汉明距离)
- 【LeetCode】461. Hamming Distance
- 【LeetCode】 461. Hamming Distance
- leetcode 461. Hamming Distance
- LeetCode-461. Hamming Distance-位运算
- leetcode#461. Hamming Distance
- LeetCode 461. Hamming Distance 知识点复习之位运算
- LeetCode :【Easy】461. Hamming Distance
- leetcode 461. Hamming Distance python实现
- leetcode 461. Hamming Distance
- LeetCode 简单操作 | 461. Hamming Distance
- LeetCode 461. Hamming Distance(Java)
- leetcode461. Hamming Distance
- 461. Hamming Distance [LeetCode]
- [Leetcode] 461. Hamming Distance 解题报告
- leetcode 461. Hamming Distance
- [leetcode]: 461. Hamming Distance