Skip to content

Commit 3d116c9

Browse files
committed
Update Number of 1 Bits.py
an efficient solution use bitwise operation
1 parent 6c34a43 commit 3d116c9

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

Number of 1 Bits.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class Solution:
22
def hammingWeight(self, n):
3-
weight = 0
4-
while n != 0:
5-
if n%2:
6-
weight += 1
7-
n = n >> 1
8-
return weight
3+
count = 0
4+
while n:
5+
count += 1;
6+
n = (n-1)&n
7+
return count

0 commit comments

Comments
 (0)