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]
.
Follow up:
It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
- Space complexity should be O(n).
- Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
分析:
这道题完全没看别人的思路,自己想出来的第一道动态规划题目,开心<( ̄︶ ̄)>
当n为奇数时,则n-1为偶数,由二进制可知,n-1的二进制最后一位必定是0
而n的二进制就是在n-1的二进制最后一位加1,并未影响n-1的前面的情况,只把最后一位0,变为了1,所以当n=奇数时,dp[n]=dp[n-1]+1;
举个栗子:6=1*(2^2)+1*(2^1)+0*(2^0),6的二进制是110.
7=1*(2^2)+1*(2^1)+1*(2^0),7的二进制是111.
当n为偶数时,先举个栗子吧,
6=1*(2^2)+1*(2^1)+0*(2^0),6的二进制是110.
n/2就是把每一项都除以2,但是你会发现每一项的系数是不变的,
6/2=3=1*(2^1)+1*(2^0)+0(2^0)
所以当n=偶数时,dp[n]=dp[n/2].
class Solution {
public:
vector<int> countBits(int num) {
vector<int> dp(num+1,0);
for(int i=1;i<num+1;i++){
if(i%2==0)
dp[i]=dp[i/2];
else
dp[i]=dp[i-1]+1;
}
return dp;
}
};
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 ...
- 338. Counting Bits题目详解
题目详情 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate ...
- 338. Counting Bits
https://leetcode.com/problems/counting-bits/ 给定一个非负数n,输出[0,n]区间内所有数的二进制形式中含1的个数 Example: For num = 5 ...
- 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 ...
随机推荐
- bzoj 2217 Lollipop
题目大意: 有一个长度为n的序列a1,a2,...,an.其中ai要么是1("W"),要么是2("T") 现在有m个询问,每个询问是询问有没有一个连续的子序列, ...
- 71.Ext.form.ComboBox 完整属性
转自:https://blog.csdn.net/taotaoqi/article/details/7409514 Ext.form.ComboBox 类全称: Ext.form.ComboBox 继 ...
- PCB genesis 大孔扩孔(不用G84命令)实现方法
PCB钻孔时,当钻刀>6.3mm时,超出钻孔范围,钻孔工序是没有这么大的钻刀,当这种情况,工程CAM会都采用G84命令用小孔扩孔的方式制作, 在这里介绍一种如果不用G84命令,用程序实现将大孔生 ...
- Django day32 跨域问题,创建vue项目,axios的使用
一:跨域问题 1.同源策略(浏览器的安全策略) 只允许当前页面朝当前域下发请求,如果向其他域发请求,请求可以正常发送,数据也可以拿回,但是被浏览器拦截了 2.cors:只要服务器实现了CORS,就可以 ...
- docker容器如何安装vim
mv /etc/apt/sources.list /etc/apt/sources.list.bak && \ echo "deb http://mirrors.16 ...
- Java_注解之二
在上一次的注解案例里面配置注解的同时,也添加了一对多(@OneToMany)的关系在里面. 本次将补充上次的缺失:其他三种关联方式的配置. 为了简化配置的复杂度 在此案例中Emp和Dept并不是唯 ...
- T-SQL查询基础
今天来带大家了解下在sql server 中的查询机制 使用select语句进行查询 1.查询所有的数据行和列 select * from student 2.查询部分行和列 select scode ...
- linq 分组
var data = from r in listRecords group r by new { r.CampaignId, r.CityId, r.Gift_DistributorId, r.Pr ...
- jQuery中国各个省份地图分部代码
jQuery中国各个省份地图分部代码 在线演示本地下载
- fcc html5 css 练习1
font-size: 字号 利用link导入新字体再引用<link href="https://fonts.gdgdocs.org/css?family=Lobster" ...