思想:
定义变量mask标志1,定义变量count计数。将n和mask相与,若为1则count+1,反之n左移一位。依次循环直到n等于0结束
class Solution(object):
def hammingWeight(self, n):
"""
:type n: int
:rtype: int
"""
mask = 1
count = 0
while n>0:
if n & mask:
count +=1
n>>=1
return count
由于之前学习了大佬的思想,现在写程序快的飞起。。。。。(自我夸奖下)