Lunski's Clutter

This is a place to put my clutters, no matter you like it or not, welcome here.

0%

191. Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of ‘1’ bits it has (also known as the Hamming weight).

Note:

1
2
Note that in some languages such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3 above, the input represents the signed integer. -3.

轉二進count 1

1
2
3
4
# T:O(n), S:O(1)  
class Solution:
def hammingWeight(self, n: int) -> int:
return bin(n).count('1')

如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Welcome to my other publishing channels