menu Lyanのブログ
Counting Bits
180 浏览 | 2020-04-28 | 分类:动态规划,数据结构与算法 | 标签:

Counting Bits

Summary

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.

My approuch

class Solution:
    def countBits(self, num: int) -> List[int]:
        if num < 2:
            return [0, 1][:num+1]
        bits = [0, 1]
        i = 2
        while i <= num:
            bits.append(bits[i//2] + i % 2)
            i += 1
        return bits
        
知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

发表评论

email
web

全部评论 (暂无评论)

info 还没有任何评论,你来说两句呐!