# -*- coding:utf-8 -*- class Solution: def NumberOf1(self, n): # write code here return sum([(n>>i & 1) for i in range(0,32)])
方法2:
# -*- coding:utf-8 -*- class Solution: def NumberOf1(self, n): # write code here count = 0 while n: count += 1 n = (n-1) & n return count
原文:https://www.cnblogs.com/tianqizhi/p/9614106.html