题目:
位1的个数:编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量)。
思路:
本题思路简单。
程序:
class Solution: def hammingWeight(self, n: int) -> int: if not n: return 0 n_str = bin(n) counter = 0 length = len(n_str) for index in range(length): if n_str[index] == '1': counter += 1 else: continue return counter