-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumber_of_1_Bits.py
More file actions
63 lines (38 loc) · 1.16 KB
/
Number_of_1_Bits.py
File metadata and controls
63 lines (38 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Built in solution with built-in function:
class Solution:
def hammingWeight(self, n):
"""
:type n: int
:rtype: int
"""
return bin(n).count('1')
'''
Using bit operation to cancel a 1 in each round
Think of a number in binary n = XXXXXX1000, n - 1 is XXXXXX0111.
n & (n - 1) will be XXXXXX0000 which is just remove the last significant 1
'''
class Solution:
def hammingWeight(self, n):
"""
:type n: int
:rtype: int
"""
c = 0
while n:
n &= n - 1
c += 1
return c
# Recursively
class Solution:
def hammingWeight(self, n, count=0):
return self.hammingWeight(n & n-1, count+1) if n!=0 else count
'''
Explanation:
Use bitwise operations:
&1 returns 1 if the last bit of the result of n>>i == 1, else 0
By using n>>i, we shift the bits to the right by i places
this way, every bit of n is the 'last bit' of n>>i at one point
sum up the number of 1s that we found that way
'''
def hammingWeight(self, n: int) -> int:
return sum((n>>i&1 for i in range(32)))