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(动态规划)的更多相关文章

  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. 338. Counting Bits题目详解

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

  5. 338. Counting Bits

    https://leetcode.com/problems/counting-bits/ 给定一个非负数n,输出[0,n]区间内所有数的二进制形式中含1的个数 Example: For num = 5 ...

  6. Java [Leetcode 338]Counting Bits

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

  7. Leetcode 338. Counting Bits

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

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

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

  9. 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 ...

随机推荐

  1. https://github.com/Boris-Em/BEMCheckBox

    https://github.com/Boris-Em/BEMCheckBox BEMCheckBox BEMCheckBox is an open source library making it ...

  2. B1297 [SCOI2009]迷路 矩阵

    这个题我觉得很有必要写一篇博客.首先,我们需要知道,假如一个邻接矩阵只有0/1构成,那么它自己的n次方就是走n步之后的方案数.但这个题还有2~9咋办呢.我们观察发现,这个题只有10个点,而且边权< ...

  3. ubuntu下如何查看和设置分辨率 (转载)

    转自:http://blog.csdn.net/jcgu/article/details/8650423 在ubuntu下可以使用xrandr来设置自己需要的分辨率.大致步骤如下: 1.使用xrand ...

  4. El Dorado(dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=2372 题意:给出n个数,求长度为m的递增子序列的数目. 思路:状态转移方程 dp[i][j] = sum(dp[ ...

  5. ckeditor使用时,第一次可以显示,修改后显示不了的问题

    1.谷歌浏览器会留有缓存,除去缓存后,就可以更改ckeditor了.下面是解决方法:

  6. [Swift通天遁地]五、高级扩展-(4)快速生成Invert、Mix、Tint、Shade颜色及调整饱和度阶

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. Centos7下安装python环境

    前言 centos7默认是装有pyhton的. #检查python版本 [root@oldboy_python ~ ::]#python -V Python 但是众所周知,python2版本到2020 ...

  8. hbuilder中的 http://www.w3.org/TR/html4/strict.dtd

    <!-- This is HTML 4.01 Strict DTD, which excludes the presentation attributes and elements that W ...

  9. 下载谷歌地图封锁IP解决办法

    采用重新拨号,动态改变IP的方式.可以使用软件<易好用IP自动更换软件>

  10. Java系列学习(十四)-集合

    1.java中的集合学习 2.Collection中常用方法 1.添加功能 boolean add(E e):添加一个元素 boolean addAll(Collection<? extends ...