题目描述:
题解:
1.count记录n中1的个数。
2.每次通过与1做按位与运算,判断n的最后一位是否为1,结果加入count,n左移一位
class Solution: def hammingWeight(self, n: int) -> int: count = 0 while n>0: bit = n&1 count = count+bit n = n>>1 return count
题目描述:
题解:
1.count记录n中1的个数。
2.每次通过与1做按位与运算,判断n的最后一位是否为1,结果加入count,n左移一位
class Solution: def hammingWeight(self, n: int) -> int: count = 0 while n>0: bit = n&1 count = count+bit n = n>>1 return count