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 ...
随机推荐
- freemarker种种
#include要使用绝对路径的话,在路径最前面加个"/"就行--<#include "/includes/v2/headerMacro.ftl"> ...
- jquery option
转--jquery动态添加option示例 http://www.jb51.net/article/45031.htm //js动态添加option var sel= document.getElem ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- [CodeIgniter] 在自定义类库中使用config配置项
通常情况下,Controller 中的方法可以通过 $this->config->item('item_name') 的方式来加载配置文件中的值 但是如果不继承 CI_Controller ...
- windows下安装kibana出 "EPERM: operation not permitted
D:\kibana-\bin>kibana-plugin install file:///x-pack-5.0.0.zip Attempting to transfer from file:// ...
- 北京培训记day4
智商题QAQ-- T1:求>=n的最小素数,n<=10^18 暴力枚举n-n+100,miller-rabin筛法 T2:给定一个01矩阵,每次选择一个1并将(x,y)到(1,1)颜色反转 ...
- Spring拦截机制之后端国际化心得
需求 前端请求的header里带有Prefer_Lang参数,向后端传递国际化信息,后端需要在处理业务之前(建立拦截机制),将Prefer_Lang保存于线程上下文. 思路分析 初次接收该需求时,为了 ...
- Beta版本冲刺第七天
Aruba 408 409 410 428 429 431 完成任务: 新增:完成文字导出为图片并改善画布大小 改进:适应MIUI系统相册选图 改进:调整activity间的跳转逻辑 改进:调整编辑窗 ...
- eclipse报错“Undefined variable from import: ...”解决方案
环境 eclipse + pydev2.8.2 + python 3.5.1/python 2.7.11 + wxpython3.0 出现原因:原先默认解释器设置为python2,重装了pydev之后 ...
- CSS样式基础总结
首行缩进:text-indent:2em 行高:line-height:1.5em 1.5倍行距 也可以设置像素文字修饰:text-decoration:underline下划线 line-throu ...