【leetcode】191. Number of 1 Bits
程序员文章站
2024-03-11 22:07:01
...
题目:
Write a function that takes an unsigned integer and return the number of ‘1’ bits it has (also known as the Hamming weight).
思路:
利用mask挨个比较每一位就好了。
代码实现:
class Solution {
public:
int hammingWeight(uint32_t n) {
uint32_t mask = 1;
int count = 0;
while (mask){
if (n & mask){
++count;
}
mask <<= 1;
}
return count;
}
};
上一篇: 微信公众平台(测试接口)准备工作
推荐阅读
-
191. Number of 1 Bits (E)
-
191. Number of 1 Bits
-
【leetcode】191. Number of 1 Bits
-
【LeetCode】762. Prime Number of Set Bits in Binary Representation
-
LeetCode - easy-762. Prime Number of Set Bits in Binary Representation
-
C++实现LeetCode(191.位1的个数)
-
Number of 1 Bits
-
【Leetcode】191. Number of 1 Bits(二进制数)(面试必备)
-
C++实现LeetCode(191.位1的个数)