Skip to content

Commit c672b38

Browse files
authored
Update counting_bits.py
1 parent 36e793b commit c672b38

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

counting_bits.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ class Solution:
66
def countBits(self, num):
77
# write your code here
88
# 这实际是个数学题。
9-
# 因为010和101的1的数量是一样的。但如果i是偶数的话,下一个数的1的数量就加1。
9+
# 1. 如果是偶数,和除2后的数的1一样多。
10+
# 2. 如果是奇数,就是前一个偶数的1的数量再加1。
1011
if num <= 0:
1112
return [0]
1213
ret = [0] * (num + 1)
13-
for i in range(1, num+1):
14-
if (i % 2) == 0:
14+
for i in range(1, num + 1):
15+
if (i % 2) != 0:
1516
ret[i] = ret[i - 1] + 1
1617
else:
17-
ret[i] = ret[i / 2]
18-
retutn ret
18+
ret[i] = ret[int(i / 2)]
19+
return ret
1920

2021
# medium: https://www.lintcode.com/problem/counting-bits

0 commit comments

Comments
 (0)