原题链接在这里:https://leetcode.com/problems/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.

题解:

Take an example: num, binary representation is 1101.

it contains two parts. The last digit, num & 1.

The other digits, 110, which has been calculated before. res[num >> 1].

Time Complexity: O(num).

Space: O(n), res array.

AC Java:

 class Solution {
public int[] countBits(int num) {
int [] res = new int[num + 1];
for(int i = 1; i <= num; i++){
res[i] = (i & 1) + res[i >> 1];
} return res;
}
}

类似Number of 1 Bits.

LeetCode Counting Bits的更多相关文章

  1. [LeetCode] Counting Bits 计数位

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

  2. LeetCode——Counting Bits

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

  3. Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits)

    Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits) 给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数 ...

  4. 【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  ...

  5. LN : leetcode 338 Counting Bits

    lc 338 Counting Bits 338 Counting Bits Given a non negative integer number num. For every numbers i ...

  6. 2016.5.16——leetcode:Reverse Bits(超详细讲解)

    leetcode:Reverse Bits 本题目收获 移位(<<  >>), 或(|),与(&)计算的妙用 题目: Reverse bits of a given 3 ...

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

  8. leetcode 上的Counting Bits 总结

    最近准备刷 leetcode  做到了一个关于位运算的题记下方法 int cunt = 0; while(temp) { temp = temp&(temp - 1);  //把二进制最左边那 ...

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

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

随机推荐

  1. SqlServer按时间自动生成生成单据编号

    SET @_tmpDateTime = GETDATE() EXEC dbo.Dtw_Common_GenerateProofCode @ProofType = 'SO',@WhsCode=@WhsC ...

  2. centos7修改主机名

    临时修改: hostname centos7 永久修改: # hostnamectl set-hostname cen07

  3. Delphi:与VCL同步(Synchronize()、用消息来同步)

    看本文时,可以同时参考:Delphi中线程类 TThread实现多线程编程(事件.临界区.Synchronize.WaitFor……) 先说一下RTL和VCL RTL(Run-Time library ...

  4. [译]:Orchard入门——使用标签管理内容

    原文链接:Organizing Content Using Tags 在Orchard中,内容项可以使用标签来分类,同时,通过格式为 ~/tags/tag-name 的url可以直接查看包含对应标签关 ...

  5. nuget packages batch install

    d:\nuget\nuget.exe install EnterpriseLibrary.Common -NoCache -Verbosity detailed -OutputDirectory D: ...

  6. 简述抽象和封装,对你学习Java有一些作用

    作为一个Java 初学者,对Java的理解可能有些片面,甚至有些错误的理解,对于观看此处的您,希望您选择性观看!!! 天知道我为什么选择学习编程,我不爱编程,但是我既然学习了,还是会努力学习的,在此分 ...

  7. [leetcode] 题型整理之动态规划

    动态规划属于技巧性比较强的题目,如果看到过原题的话,对解题很有帮助 55. Jump Game Given an array of non-negative integers, you are ini ...

  8. Is It A Tree?[HDU1325][PKU1308]

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  9. ZeroMQ接口函数之 :zmq_disconnect - 断开一个socket的连接

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_disconnect zmq_disconnect(3) ØMQ Manual - ØMQ/3.2.5 Name ...

  10. dedecms中调用制定栏目

    {dede:type typeid='5'} <li> <a href="[field:typelink/]" target="_blank" ...