leetcode-461(Hamming Distance)
2017-06-02 10:43
399 查看
Description
The Hamming distance between two integers is the number of positions at which the corresponding bits are
different.
Given two integers
calculate the Hamming distance.
Note:
0 ≤
231.
Example:
Solution
public class Solution {
public int hammingDistance(int x, int y) {
return Integer.bitCount(x^y);
}
}
Conclude
海明距离就是计算两个数异或后的结果中有多少个1.
The Hamming distance between two integers is the number of positions at which the corresponding bits are
different.
Given two integers
xand
y,
calculate the Hamming distance.
Note:
0 ≤
x,
y<
231.
Example:
Input: x = 1, y = 4 Output: 2 Explanation: 1 (0 0 0 1) 4 (0 1 0 0) ↑ ↑ The above arrows point to positions where the corresponding bits are different.
Solution
public class Solution {
public int hammingDistance(int x, int y) {
return Integer.bitCount(x^y);
}
}
Conclude
海明距离就是计算两个数异或后的结果中有多少个1.
相关文章推荐
- Leetcode 461 Hamming Distance 自制答案
- leetcode解题之461 # Hamming Distance Java版
- 位操作-leetcode 461 Hamming Distance
- 【LeetCode】461 Hamming Distance
- LeetCode(461) Hamming Distance
- LeetCode_461-hamming distance
- 从0开始的LeetCode生活—461-Hamming Distance(汉明距离)
- leetcode 461: Hamming Distance
- leetcode--461 : Hamming Distance
- leetcode 461:Hamming Distance
- LeetCode461 Hamming Distance JAVA题解
- LeetCode@Bit_461_Hamming_Distance
- leetcode-461 Hamming Distance
- [LeetCode461]Hamming Distance
- leetcode 461 hammingDistance
- python leetcode461 汉明距离
- leetcode461
- LeetCode:461 Hamming Distance
- C++ Solution of LeetCode 461 Hamming Distance
- LeetCode 461 191 477