Problem:
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.
Analysis:
Solution:
C++:
int hammingWeight(uint32_t n) {
int num = 0;
for(; n > 0; n /= 2)
num += (n % 2);
return num;
}
Java:
Python:
最后
以上就是满意期待最近收集整理的关于[Leetcode 191, Easy] Number of 1 Bits的全部内容,更多相关[Leetcode内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复