leetcode 461.Hamming Distance(easy)[位运算]
2016-12-21 11:37
316 查看
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:
class Solution {
public:
int hammingDistance(int x, int y) {
int n = x^y;
int count = 0;
while(n)
{
++count;
n = n &(n-1);
}
return count;
}
};
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.
class Solution {
public:
int hammingDistance(int x, int y) {
int n = x^y;
int count = 0;
while(n)
{
++count;
n = n &(n-1);
}
return count;
}
};
相关文章推荐
- LeetCode(一)--461.Hamming Distance&476.Number Complement 位运算
- 【LeetCode】461.Hamming Distance_EASY(一)
- LeetCode-461. Hamming Distance-位运算
- 【LeetCode】(C#)461. Hamming Distance(Easy)
- leetcode解题报告--461.Hamming Distance
- 【leetcode】【Easy】【461. Hamming Distance】【bit manipulation】
- [LeetCode]461.Hamming Distance
- LeetCode :【Easy】461. Hamming Distance
- [LeetCode]461.Hamming Distance
- Leetcode 461.Hamming Distance
- [LeetCode] 461.Hamming Distance 备忘
- leetcode 461. Hamming Distance 汉明距离 + 位运算
- 【leetcode】【Easy】【453. Minimum Moves to Equal Array Elements】【math】
- LeetCode :【Easy】412. Fizz Buzz
- Leetcode243: Shortest Word Distance
- Leetcode Note: Linked List Easy Section Part 2
- leetcode_155_Min Stack(C++)(easy)
- [Leetcode 21, Easy] Merge Two Sorted Lists
- Leetcode 317. Shortest Distance from All Buildings(多次BFS)
- leetcode Hamming Distance 汉明距离