leetCode #191 number of 1 bits

来源:互联网 发布:phpcms建站 编辑:程序博客网 时间:2024/05/19 15:44

题目:统计32位整型二进制1的位数

分析:按位判断即可。

答案:

class Solution {public:    int hammingWeight(uint32_t n) {        int result = 0;        while(n)        {            if (n& 1)                result ++;            n = n>>1;        }        return result;    }};

0 0
原创粉丝点击