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. Python操作redis系列以 哈希(Hash)命令详解(四)

    # -*- coding: utf-8 -*- import redis #这个redis不能用,请根据自己的需要修改 r =redis.Redis(host=") 1. Hset 命令用于 ...

  2. php比较函数,判断安全函数

    一.字符串比较函数: int strcasecmp ( string $str1 , string $str2 ) int strcmp ( string $str1 , string $str2 ) ...

  3. spawn-fcgi原理及源代码分析

    spawn-fcgi是一个小程序,作用是管理fast-cgi进程,功能和php-fpm类似,简单小巧,原先是属于lighttpd的一部分.后来因为使用比較广泛.所以就迁移出来作为独立项目了.本文介绍的 ...

  4. iOS之美: UIView 与 UIWindow之间的关系

    转自:http://leopard168.blog.163.com/blog/static/168471844201381584533466/ 面对iOS初学者,总会被问到一些不常被关注的问题,比如: ...

  5. 打印十字图 queue 搞定

    题目描述 小明为某机构设计了一个十字型的徽标(并非红十字会啊),如下所示: ..$$$$$$$$$$$$$.. ..$...........$.. $$$.$$$$$$$$$.$$$ $...$... ...

  6. jquery的liveQuery插件

    一.livequery插件简介 jQuery的事件绑定功能使得jQuery代码与HTML代码能够完全分离,这样代码的层次关系更加清晰,维护起来也更加简单.然而对于动态加载到页面的HTML元素,每次都需 ...

  7. ubuntu和pypi换源

    ubuntu用apt-get下载的源是可以更换的.之前一直是打开软件中心在编辑里找源,找到后系统会自动备份原来的源并换源.奇怪却搜不到自己学校的源=.= 想换源还有一个原因,之前在update的时候会 ...

  8. ThreadPool has stuck threads

    weblogic 10后台出现警告,原因:ThreadPool has stuck threads 在WEBLOGIC中如果一个线程执行时间超过了Stuck Thread Max Time规定的时间, ...

  9. GPIO 输入—按键检测

    这里要用到一定的模电知识.电容两端电压不能突变,电感两端电流不能突变.这里利用了电容的放电延时实现硬件消抖.按键按下会有抖动,波形有毛刺,使得高低电平显现不明显,而按键按下时,电容放电一下,马上又被充 ...

  10. java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明)

    转载地址:http://www.devba.com/index.php/archives/4581.html java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明); ...