Skip to content

Commit a4103a6

Browse files
committed
Update counting-bits.py
1 parent 6e7eb27 commit a4103a6

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

Python/counting-bits.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Time: O(n)
2-
# Space: O(1)
2+
# Space: O(n)
33

44
# Given a non negative integer number num. For every numbers i
55
# in the range 0 <= i <= num calculate the number
@@ -33,5 +33,6 @@ def countBits(self, num):
3333
"""
3434
res = [0]
3535
for i in xrange(1, num + 1):
36-
res.append(res[i >> 1] + (i & 1))
36+
# Number of 1's in i = (i & 1) + number of 1's in (i / 2).
37+
res.append((i & 1) + res[i >> 1])
3738
return res

0 commit comments

Comments
 (0)