我是靠谱客的博主 满意期待,这篇文章主要介绍[Leetcode 191, Easy] Number of 1 Bits,现在分享给大家,希望可以做个参考。

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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(142)

评论列表共有 0 条评论

立即
投稿
返回
顶部