边工作边刷题:70天一遍leetcode: day 3

Number of 1 Bits

思路:
速度:

For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.

class Solution(object):
    def hammingWeight(self, n):
        """
        :type n: int
        :rtype: int
        """
        count = 0
        while n:
            n = n&(n-1)
            count+=1
        return count
posted @ 2016-05-25 23:29  absolute100  阅读(124)  评论(0编辑  收藏  举报