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.

Analysis:

0 | 0+1| 0+1 1+1| 0+1 1+1 1+1 2+1 |..........

0 | 1    | 1       2  | 1      2    2     3     |................

Solution:

public class Solution {
public int[] countBits(int num) {
int[] res = new int[num + 1]; res[0] = 0;
int nextNum = 1;
int nextCount = 1;
while (nextNum <= num) {
for (int i = 0; i < nextCount && nextNum <= num; i++) {
res[nextNum++] = res[i] + 1;
}
nextCount *= 2;
}
return res;
}
}

LeetCode-Count Bits的更多相关文章

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

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

  2. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  3. [LeetCode] Counting Bits 计数位

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

  4. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  5. [LeetCode] Count of Smaller Numbers After Self 计算后面较小数字的个数

    You are given an integer array nums and you have to return a new counts array. The counts array has ...

  6. [LeetCode] Count Univalue Subtrees 计数相同值子树的个数

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  7. [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  8. [LeetCode] Count Primes 质数的个数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  9. [LeetCode] Reverse Bits 翻转位

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  10. LeetCode Counting Bits

    原题链接在这里:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. Fo ...

随机推荐

  1. hibernate 中集合的保存

    一.开发流程 1)引入jar包,注意引入数据库驱动包 2)创建数据库表 //创建用户表 CREATE TABLE USER( id INT PRIMARY KEY AUTO_INCREMENT, un ...

  2. tbnet编译

    下载tbnet 下载地址:http://code.taobao.org/p/tb-common-utils/src/trunk/tbnet/ ,它的svn地址为:http://code.taobao. ...

  3. 【Android】15.0 第15章 广播和通知—本章示例主界面

    分类:C#.Android.VS2015: 创建日期:2016-02-28 一.简介 广播(Broadcast):其功能类似于收音机的广播,你只要调到那个台(只要在接收的类中注册了要接收的广播),就能 ...

  4. windows 添加打印机

    控制面板---->硬件和声音---->设备和打印机--->点击添加打印机 最后安驱动(选择通用) OK!

  5. 如何在Windows下面运行hadoop的MapReduce程序

    在Windows下面运行hadoop的MapReduce程序的方法: 1.下载hadoop的安装包,这里使用的是"hadoop-2.6.4.tar.gz": 2.将安装包直接解压到 ...

  6. Using JWT with Spring Security OAuth

    http://www.baeldung.com/spring-security-oauth-jwt ************************************************** ...

  7. Debian8.0 搭建leanote

    参考了官方wiki以及中文博客 https://github.com/leanote/leanote/wiki http://leanote.leanote.com/post/Leanote-manu ...

  8. oracle ORA-12545:因目标主机或对象不存在

    解决方法: 1.首先从最基本的入手,这里打开计算机右击,选择管理 2. 找到里面的服务和应用程序,打开服务 3.找到: OracleOraDb11g_home1TNSListener OracleSe ...

  9. Js 省市联动

    function cn(){ this.Items = {}; } cn.prototype.add = function(id,iArray){ this.Items[id] = iArray; } ...

  10. Cookie、Session详解

    讲解的很全面 https://www.cnblogs.com/andy-zhou/p/5360107.html