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. 解决Enter键与input 、a标签触发的事件的冲突

    无论是 <button type="button" onclick="console.log('123');">123</button> ...

  2. centos vsftp 500 OOPS: cannot change directory

    CentO中把vsftpd安裝配置好了,以為大功告成,但用FTP 登入出現下錯誤:500 OOPS: cannot change directoryCentOS系統安裝了SELinux,因為預設下是沒 ...

  3. mysqldump全量备份+mysqlbinlog二进制日志增量备份

    日常的数据备份及恢复测试,是DBA工作重中之重的事情,所以要做好备份及测试,日常的备份常见有mysqldump+binlog备份.xtrabackup+binlog备份,无论那一种,几乎都少不了对bi ...

  4. 0073 javacTask: 源发行版 1.8 需要目标发行版 1.8

    今天在编译执行下面这段代码的时候,编译报错:javacTask: 源发行版 1.8 需要目标发行版 1.8 public class Test { public static void main(St ...

  5. .NET Standard - 揭秘 .NET Core 和 .NET Standard[转自MSDN]

    作为 .NET 系列的最新成员,.NET Core 和 .NET Standard 的概念及其与 .NET Framework 的区别并不十分明确.在本文中,我将准确介绍每个产品及其适用场景. 在详细 ...

  6. qt setData()和data()

    简述 在GUI开发中,往往需要在界面中存储一些有用的数据,这些数据可以来自配置文件.注册表.数据库.或者是Server. 无论来自哪里,这些数据对于用户来说都是至关重要的,它们在交互过程中大部分都会被 ...

  7. 网络相关命令-netstat

    网络相关命令 netstat显示网络状态 usage: netstat [-vWeenNcCF] [<Af>] -r netstat {-V|--version|-h|--help} ne ...

  8. Gogs http和ssh地址显示localhost的问题

    问题描述: 如下图所示HTTP和SSH地址显示的是localhost而不是对应的域名或地址. 解决方案: 按以下方法修改Gogs配置文件并重启服务器 域名问题 RUN_USER = git DOMAI ...

  9. python的zipfile实现文件目录解压缩

    主要是 解决了压缩目录下 空文件夹 的压缩 和 解压缩问题 压缩文件夹的函数: # coding:utf- import os import zipfile def zipdir(dirToZip,s ...

  10. 得到application

    ServletContext application = request.getSession().getServletContext();        String basePath = (Str ...