LeetCode338 比特位计数

LeetCode338 比特位计数

class Solution:
    def countBits(self, n: int) -> List[int]:

        def cnt(x: int) -> int:
            ones = 0
            while x > 0:
                x = x & (x - 1)
                ones = ones + 1
            return ones

        ans = [cnt(i) for i in range(n + 1)]
        return ans

posted on 2022-08-24 16:56  solvit  阅读(22)  评论(0)    收藏  举报

导航