338. Counting Bits
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的更多相关文章
- LN : leetcode 338 Counting Bits
lc 338 Counting Bits 338 Counting Bits Given a non negative integer number num. For every numbers i ...
- 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 ...
- 【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 ...
- Java [Leetcode 338]Counting Bits
题目描述: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculat ...
- Leetcode 338. Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
- leetcode 338. Counting Bits,剑指offer二进制中1的个数
leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种 ...
- 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 ...
- 338 Counting Bits Bit位计数
给定一个非负整数 num. 对于范围 0 ≤ i ≤ num 中的每个数字 i ,计算其二进制数中的1的数目并将它们作为数组返回.示例:比如给定 num = 5 ,应该返回 [0,1,1,2,1,2] ...
- 338. Counting Bits(动态规划)
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
随机推荐
- Linux 安装MySQL
安装配置 [root@iZ28gvqe4biZ ~]# rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.r ...
- neo4j-简介,安装
1. Neo4j简介 Neo4j是一个用Java实现的.高性能的.NoSQL图形数据库. Neo4j 使用图(graph)相关的概念来描述数据模型,通过图中的节点和节点的关系来建模. Neo4j完全兼 ...
- PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案
$s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...
- String 字符串
多行字符串: http://php.net/manual/zh/language.types.string.php#language.types.string.syntax.nowdoc DEMO: ...
- oracle 修改字符集支持中文
1.登录sys关闭服务 [oracle@t-e ~]$ export ORACLE_SID=kamiltest1 [oracle@t-e ~]$ sqlplus / as sysdba SQL> ...
- Javascript数组操作(转)
1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限, ...
- 文档分享-Activiti 5.16 用户手册
今天在翻看工作流相关的网页的时候,在开源中国上http://www.oschina.net/question/915507_149175发现activiti的中文文档:http://www.mossl ...
- 怎么把电脑的word,txt,pdf等文件拷贝到iPhone手机上
之前都是用的qq什么的传文件,电脑发送到qq上.今天尝试了一下用itunes把电脑上的文件夹弄到iPhone上. 1.首先,打开电脑的偏好设置,找到共享如图: 打开它,勾选文件共享. 2.把手机和电脑 ...
- fedora自带OpenJDK,所以如果安装官方的JDK的话要先删除OpenJDK
1:rpm -qa|grep jdk 查看当前的jdk情况. 2:yum -y remove java java-1.7.0-openjdk* 卸载openjdk,这个过程中因为依赖原因可能会卸载一些 ...
- Canvas绘制时钟
①首先在HTML的body标签中添加一个canvas标签,用于绘制时钟. <canvas id="myCanvas" width="600" height ...