We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 36e793b commit c672b38Copy full SHA for c672b38
counting_bits.py
@@ -6,15 +6,16 @@ class Solution:
6
def countBits(self, num):
7
# write your code here
8
# 这实际是个数学题。
9
- # 因为010和101的1的数量是一样的。但如果i是偶数的话,下一个数的1的数量就加1。
+ # 1. 如果是偶数,和除2后的数的1一样多。
10
+ # 2. 如果是奇数,就是前一个偶数的1的数量再加1。
11
if num <= 0:
12
return [0]
13
ret = [0] * (num + 1)
- for i in range(1, num+1):
14
- if (i % 2) == 0:
+ for i in range(1, num + 1):
15
+ if (i % 2) != 0:
16
ret[i] = ret[i - 1] + 1
17
else:
- ret[i] = ret[i / 2]
18
- retutn ret
+ ret[i] = ret[int(i / 2)]
19
+ return ret
20
21
# medium: https://www.lintcode.com/problem/counting-bits
0 commit comments