We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6e7eb27 commit a4103a6Copy full SHA for a4103a6
Python/counting-bits.py
@@ -1,5 +1,5 @@
1
# Time: O(n)
2
-# Space: O(1)
+# Space: O(n)
3
4
# Given a non negative integer number num. For every numbers i
5
# in the range 0 <= i <= num calculate the number
@@ -33,5 +33,6 @@ def countBits(self, num):
33
"""
34
res = [0]
35
for i in xrange(1, num + 1):
36
- res.append(res[i >> 1] + (i & 1))
+ # Number of 1's in i = (i & 1) + number of 1's in (i / 2).
37
+ res.append((i & 1) + res[i >> 1])
38
return res
0 commit comments