美文网首页
191 Number of 1 Bits

191 Number of 1 Bits

作者: Closears | 来源:发表于2015-08-06 21:34 被阅读14次

原题链接:Number of 1 Bits

感受一下Python的强大吧!!!
代码如下:

class Solution:
    # @param n, an integer
    # @return an integer
    def hammingWeight(self, n):
        count = 0
        num = list(bin(n))
        for bit in num:
            if bit is '1':
                count += 1
        return count

相关文章

网友评论

      本文标题:191 Number of 1 Bits

      本文链接:https://www.haomeiwen.com/subject/tukbqttx.html