力扣 338. 比特位计数

解法1

1
2
3
4
5
6
7
8
9
10
11
12
def countBits(n):
def count(x):
if x == 0: return 0
count_ = 0
while x > 0:
x &= (x - 1)
count_ += 1
return count_
ret = []
for i in range(n + 1):
ret.append(count(i))
return ret
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信