二进制有多少个1

经典题目

# Number of 1 Bits
# 00000000000000000000000000001011,return 3.
# 2(00000000000000000000000000000010) return 1.

class Solution:
    # @param n, an integer
    # @return an integer
    def hammingWeight(self, n):
        nn=n
        count=0
        while nn:
            count+=1
            nn=nn&(nn-1)
        return count
posted @ 2015-04-29 09:21  clq.lib  阅读(156)  评论(0编辑  收藏  举报