今天跑去跳韓舞,
好久沒有一次跳四小,
覺得骨頭都快散掉了qwq
而且今天也解不了好多題,
只能找些簡單的拿來放了(跪
Write a function that takes an integer as input, and returns the number of bits that are equal to one in the binary representation of that number. You can guarantee that input is non-negative.
Example: The binary representation of 1234 is 10011010010, so the function should return 5 in this case
就是乖乖使用 python 裡面的 bin() 方法可以直接將數字轉成二進制字串,
雖然前方有 '0b' 開頭,
但不影響題目中所要求的 '1' 的數量~
def countBits(n):
return bin(n).count('1')
另一題我會盡快補上qwq