Number of 1 Bits

来源:互联网 发布:项目数据分析公司排名 编辑:程序博客网 时间:2024/06/13 21:34

c++

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

python

class Solution(object):    def hammingWeight(self, n):        """        :type n: int        :rtype: int        """        bn = bin(n)[2:]        return bn.count('1')
0 0