LeetCode "Counting Bits"

Neat DP problem to go.

from math import *

class Solution(object):
    def countBits(self, num):
        ret = [0]
        for i in range(1, num + 1):
            if i & 1: # odd
                ret += [ret[-1] + 1]
            else:
                prev = i - 1
                trailing1s = (i | prev) - i
                cnt = int(math.log(trailing1s + 1, 2))
                ret += [ret[-1] + 1 - cnt]
        return ret

If you don‘t like log(), you can use lookup table.

时间: 2024-10-14 08:41:47

LeetCode "Counting Bits"的相关文章

[LeetCode][Java][JavaScript]Counting Bits

Counting Bits 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. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

leetcode(1)--338.Counting Bits

LeetCode 338. Counting Bits 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. Example: For num = 5 you should return [0,1,1,2,

【LeetCode】338. Counting Bits (2 solutions)

Counting Bits 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. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

leetCode 338. Counting Bits | Dynamic Programming | Medium

338. Counting Bits 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. Example:For num = 5 you should return [0,1,1,2,1,2]. 题目大意

[LeetCode]Reverse Bits

题目:Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through thi

2016.5.16——leetcode:Reverse Bits(超详细讲解)

leetcode:Reverse Bits 本题目收获 移位(<<  >>), 或(|),与(&)计算的妙用 题目: Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in bin

338. Counting Bits &amp;&amp; 191. Number of 1 Bits

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. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up: It is very e

【Leetcode 338】 Counting Bits

---恢复内容开始--- 问题描述:给出一个非负整数num,对[0, num]范围内每个数都计算它的二进制表示中1的个数 Example:For num = 5 you should return [0,1,1,2,1,2] 思路:这种题适合归纳法,找出规律然后用编程语言描述,令i从0开始,设f(i)为i对应二进制表示中1的个数 也就是每次有一个大小为2^n的序列,把他们每个数都加1,然后插入到结尾就构成了大小为2^(n+1)个序列. 直到2^(n+1)比num大,取前num个数组成序列返回即可

leetcode 上的Counting Bits 总结

最近准备刷 leetcode  做到了一个关于位运算的题记下方法 int cunt = 0; while(temp) { temp = temp&(temp - 1);  //把二进制最左边那个1变为零 count++;   //统计1的个数 } 同理把位二进制坐左边那个0变为1 就可以  temp = temp|(temp + 1)