Leetcode代码学习周记——Hamming Distance
2017-12-10 21:49
351 查看
题目链接:
https://leetcode.com/problems/hamming-distance/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.
给定两个整数,记录两个整数的二进制表示下每一位出现了不同的情况。
二进制位不同,自然而然想到了位运算的异或
class Solution {
public:
int hammingDistance(int x, int y) {
int z = x ^ y;
int count = 0;
while (z != 0) {
if (z & 1) count++;
z >>= 1;
}
return count;
}
};
本题主要要求掌握位运算
提交结果:
https://leetcode.com/problems/hamming-distance/description/
题目描述:
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.
给定两个整数,记录两个整数的二进制表示下每一位出现了不同的情况。
二进制位不同,自然而然想到了位运算的异或
class Solution {
public:
int hammingDistance(int x, int y) {
int z = x ^ y;
int count = 0;
while (z != 0) {
if (z & 1) count++;
z >>= 1;
}
return count;
}
};
本题主要要求掌握位运算
提交结果:
相关文章推荐
- Leetcode代码学习周记——Palindrome Number
- Leetcode代码学习周记——Two Sum
- Leetcode代码学习周记——Sqrt(x)
- Leetcode代码学习周记——Container With Most Water
- Leetcode代码学习周记——Merge Two Binary Trees
- Leetcode代码学习周记——Self Dividing Numbers
- Leetcode代码学习周记——Judge Route Circle
- Leetcode代码学习周记——Number Complement
- Leetcode代码学习周记——Add Two Nmubers
- Leetcode代码学习周记——Longest Palindromic Substring
- Leetcode代码学习周记——Longest Substring Without Repeating Characters
- Leetcode代码学习周记——Longest Common Prefix
- Leetcode代码学习周记——3Sum
- Leetcode代码学习周记——Find Anagram Mappings
- Leetcode代码学习周记——Reverse Integer
- leetcode学习其他人的代码
- JChat 代码学习
- [LIBGDX学习]LibGDX代码详解(三)Box2D character control
- 黑马程序员_C#学习之数组(代码)
- 步步为营 .NET 代码重构学习笔记 五、分解函数和替换算法(Replace Method And Substitute Algorithm)