您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode 算法