赞
踩
Write a function that takes an unsigned integer and returns the number of ‘1’ bits it has (also known as the Hamming weight).
Note:
Counting the remainder of division by two.
class Solution:
def hammingWeight(self, n: int) -> int:
ans = 0
while n:
ans = ans + n % 2
n = n // 2
return ans
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。