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. luogu 1939 【模板】矩阵加速(数列)

    题目大意: a[1]=a[2]=a[3]=1 a[x]=a[x-3]+a[x-1] (x>3) 求a数列的第n项%1000000007 思路: 使用矩阵快速幂进行加速 在草稿纸上填了填数 然后就 ...

  2. 同一个Tomcat下不同项目之间的session共享

    最近发现项目运行过程中经常会抛出一个 NullPointerException的异常,经检查发现异常出现的地方是日志模板,一阵检查,正常无误 (把所有记录日志的地方都点了一遍,心里是崩溃的),万念俱灰 ...

  3. eclipse中Kotlin的基础应用

    最近逛网站时无意中发现有一门新语言谈论很广-- kotlin ,能够完全兼容Java.这就引起了楼主的好奇心,据所周知,Java就是因为多平台的支持 才流行起来.OK,闲话不多说,下面看图讲代码. 1 ...

  4. [Swift通天遁地]二、表格表单-(9)快速创建一个美观强大的表单

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

  5. [Swift通天遁地]七、数据与安全-(15)使用单元测试进行代码的性能分析

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

  6. 机器学习——Day 3 多元线性回归

    写在开头 由于某些原因开始了机器学习,为了更好的理解和深入的思考(记录)所以开始写博客. 学习教程来源于github的Avik-Jain的100-Days-Of-MLCode 英文版:https:// ...

  7. android 中的Context(一)

    context的功能如此强大,它是activity的父类. public abstract class Context { ... public abstract Object getSystemSe ...

  8. 【洛谷2624_BZOJ1005】[HNOI2008] 明明的烦恼(Prufer序列_高精度_组合数学)

    题目: 洛谷2624 分析: 本文中所有的 "树" 都是带标号的. 介绍一种把树变成一个序列的工具:Prufer 序列. 对于一棵 \(n\) 个结点的树,每次选出一个叶子(度数为 ...

  9. Coursera公开课-Machine_learing:编程作业7

    这周的编程作业主要是两方面内容. 1.K-means聚类. 2.PCA(Principle Component Analys)主成分分析. 方式主要是通过对图像的聚类实现压缩图像,后来发现PCA也可以 ...

  10. Scala-基础-数据类型

    import junit.framework.TestCase import org.junit.Test import scala.runtime.RichByte //数据类型 class Dem ...