/*
* Leetcode/*
* Leetcode**191. Number of 1 Bits**
* Funtion: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
* Example: the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.
* Author: LKJ
* Date: 2017/2/27
* Hint: 汉明距离问题的解决步骤之一,找到一个整数中bit为1的数量
*/
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
class Solution {
public:
int hammingWeight(uint32_t n) {
int result = 0;
while(n){
if(n & 1){
result++;
}
n = n >> 1;
}
return result;
}
};
int main(){
uint32_t myin = 11;
int myout;
Solution SA;
myout = SA.hammingWeight(myin);
cout << myout << endl;
return 0;
}
LeetCode 简单操作 | 191. Number of 1 Bits
最新推荐文章于 2020-02-24 13:26:12 发布