https://leetcode.com/problems/counting-bits/

给定一个非负数n,输出[0,n]区间内所有数的二进制形式中含1的个数

Example:
For num = 5 you should return [0,1,1,2,1,2].

注意fellow up部分,题目说了你要是一个个无脑去遍历输出是不ok的,直接用某些内置函数也是不行的

解题思路

实在没思路就看看hint部分

找张纸,多写几个数,包括:

1、数(十进制)

2、数(二进制)

3、二进制中1的个数

图片来自http://blog.csdn.net/qiexingqieying/article/details/51719997

感谢作者keke he,侵删

横线分组,会发现第1组的count是第0组的count+1,第n组是1+2+。。。+(n-1)组的count+1

所以某个数的count就是这个数减所在组2开方数的count+1

res[x]=res[x-pow(2,len(bin(x))-3)]+1

注意python的bin()结果强制带前缀0b

然后类似斐波拉切数列,第一个和第二个强制制定

class Solution(object):
def countBits(self, num):
res=[1] * (num+1)
if num==0:
res[0]=0
elif num==1:
res[0]=0
res[1]=1
elif num>1:
res[0] = 0
res[1] = 1
for x in xrange(2,num+1):
res[x]=res[x-pow(2,len(bin(x))-3)]+1
return res

338. Counting Bits的更多相关文章

  1. LN : leetcode 338 Counting Bits

    lc 338 Counting Bits 338 Counting Bits Given a non negative integer number num. For every numbers i ...

  2. Week 8 - 338.Counting Bits & 413. Arithmetic Slices

    338.Counting Bits - Medium Given a non negative integer number num. For every numbers i in the range ...

  3. 【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  ...

  4. Java [Leetcode 338]Counting Bits

    题目描述: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculat ...

  5. Leetcode 338. Counting Bits

    Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...

  6. leetcode 338. Counting Bits,剑指offer二进制中1的个数

    leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种 ...

  7. Leet Code OJ 338. Counting Bits [Difficulty: Medium]

    题目: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate ...

  8. 338 Counting Bits Bit位计数

    给定一个非负整数 num. 对于范围 0 ≤ i ≤ num 中的每个数字 i ,计算其二进制数中的1的数目并将它们作为数组返回.示例:比如给定 num = 5 ,应该返回 [0,1,1,2,1,2] ...

  9. 338. Counting Bits(动态规划)

    Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...

随机推荐

  1. js中的原型prototype

    var arr1 = new Array(12,34,98,43,38,79,56,1); arr1.sum=function (){ var result = 0; for(var i=0; i&l ...

  2. 【BZOJ 1005】【HNOI 2008】明明的烦恼

    http://www.lydsy.com/JudgeOnline/problem.php?id=1005 答案是\[\frac{(n-2)!}{(n-2-sum)!×\prod_{i=1}^{cnt} ...

  3. PL/SQL数据库,Oracle登录

    用户名:TESTZYPX_9999 数据库:10.75.142.242:1521/orcl

  4. hibernate主键生成策略

    在hibernate中,提供了多种主键生成器(不同的数据库,不同的表结构使用的主键生成策略也不相同),查阅相关资料经过实验总结如下: 1.increment 主键按照数值顺序递增,使用当前实例中最大值 ...

  5. 用hibernate tools生成对应的sql应用代码

    参考资料: eclipse在线配置hibernate tools http://jingyan.baidu.com/article/db55b609959d154ba20a2f5d.html [图]H ...

  6. Unity Animator动画状态机 深入理解(三)二维混合树

    介绍二维之前,先说说一维吧~ 这个是通过旋转角度速度快慢来表现身体的大转和中转~ 通过一个-133~133的数值来进行控制. 注:后面的那个对钩是镜像的意思. 其实二维混合树并没有想象中的那么难.先来 ...

  7. 他(he)(钟神)

    他[问题描述]一张长度为N的纸带,我们可以从左至右编号为0 −N(纸带最左端标号为0) .现在有M次操作,每次将纸带沿着某个位置进行折叠,问所有操作之后纸带的长度是多少.[输入格式]第一行两个数字N, ...

  8. HibernateSessionFactory建立-使用ThreadLocal

    立即加载还是延迟加载必须要连接数据库的,而在Java中连接数据库是依赖java.sql.Connection,在hibernate中session就是Connection的一层高级封装,一个sessi ...

  9. java中获取路径的几种基本的方法

    package com.ygh.blog.realpath; import java.io.File; import java.io.IOException; import java.io.Input ...

  10. 将一张表的数据,拷贝到另一张表中sql

    两张表的字段一样 create table 目标表 as  select * from 原表;