LeetCode 简单操作 | 461. Hamming Distance
2017-03-02 11:15
302 查看
/* * Leetcode461. Hamming Distance * Funtion: 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. 0 ≤ x, y < 2^31. * Example: Input: x = 1, y = 4 Output: 2 * Author: LKJ * Date: 2017/2/27 * Hint: 汉明距离,找出不相同的比特数有几个 */ #include <iostream> #include <vector> #include <string> #include <cmath> #include <algorithm> using namespace std; class Solution { public: int hammingDistance(int x, int y) { int z = x^y; int result = 0; while(z){ if(z & 1){ result++; } z = z >> 1; } return result; } }; int main(){ int myin1 = 2; int myin2 = 4; int myout; Solution SA; myout = SA.hammingDistance(myin1,myin2); cout << myout << endl; return 0; }
相关文章推荐
- Leetcode461. Hamming Distance不要太简单哦
- Leetcode#461. Hamming Distance(异或)
- leetcode - 461. Hamming Distance (bit mannipulation)
- LeetCode | 461. Hamming Distance 12_16
- LeetCode 简单操作 | 171. Excel Sheet Column Number
- LeetCode :【Easy】461. Hamming Distance
- [LeetCode]461. Hamming Distance
- leetcode----461. Hamming Distance
- Leetcode题解 - 461. Hamming Distance
- [LeetCode] 461. Hamming Distance(位操作)
- 【LeetCode】(C#)461. Hamming Distance(Easy)
- [Leetcode从零开刷]461. Hamming Distance
- LeetCode: 461. Hamming Distance
- Leetcode 461. Hamming Distance
- leetCode(Using C)——461. Hamming Distance
- leetcode 461. Hamming Distance
- LeetCode——461. Hamming Distance(C++,模拟)
- LeetCode 461. Hamming Distance
- LeetCode 简单操作 | 191. Number of 1 Bits
- LeetCode 简单操作 | 389. Find the Difference