题目:

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.

Hint:

You should make use of what you have produced already.

翻译:

给定一个非负整数num,对于每一个0<=i<=num的整数i。计算i的二进制表示中1的个数,返回这些个数作为一个数组。

比如。输入num = 5 你应该返回 [0,1,1,2,1,2].

分析:

依照常规思路,非常容易得出“Java代码2”的方案。可是这个方法的时间复杂度是O(nlogn)。

通过对数组的前64个元素进行分析(num=63),我们发现数组呈现一定的规律,不断重复。例如以下图所看到的:

0
1
1 2
1 2 2 3
1 2 2 3 2 3 3 4
1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5
1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 3 3 4 3 4 4 5 3 4 4 5 4 5 5 6

由此我们发现0112是一个基础元素。不断循环重复。能够推论:假设已知第一个元素是result[0],那么第二第三个元素为result[0]+1,第四个元素为result[0]+2,由此获得前4个元素result[0]~result[3]。以这4个元素为基础。我们能够得到

result[4]=result[0]+1,result[5]=result[1]+1…。

result[8]=result[0]+1,result[9]=result[1]+1… ,

result[12]=result[0]+2,result[13]=result[1]+2…;

以此类推能够获得所有的数组。

Java版代码1:

public class Solution {
public int[] countBits(int num) {
int[] result = new int[num + 1];
int range = 1;
result[0] = 0;
boolean stop = false;
while (!stop) {
stop = fillNum(result, range);
range *= 4;
}
return result;
} public boolean fillNum(int[] nums, int range) {
for (int i = 0; i < range; i++) {
if (range + i < nums.length) {
nums[range + i] = nums[i] + 1;
} else {
return true;
}
if (2 * range + i < nums.length) {
nums[2 * range + i] = nums[i] + 1;
}
if (3 * range + i < nums.length) {
nums[3 * range + i] = nums[i] + 2;
}
}
return false;
}
}

Java版代码2:

public class Solution {
public int[] countBits(int num) {
int[] result=new int[num+1];
result[0]=0;
for(int i=1;i<=num;i++){
result[i]=getCount(i);
}
return result;
}
public int getCount(int num){
int count=0;
while(num!=0){
if((num&1)==1){
count++;
}
num/=2;
}
return count;
}
}

Leet Code OJ 338. Counting Bits [Difficulty: Medium]的更多相关文章

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

  2. LN : leetcode 338 Counting Bits

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

  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. Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]

    题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 思路分析: 题意是将二叉树全部左右子数 ...

  5. Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  6. Leet Code OJ 237. Delete Node in a Linked List [Difficulty: Easy]

    题目: Write a function to delete a node (except the tail) in a singly linked list, given only access t ...

  7. Leet Code OJ 26. Remove Duplicates from Sorted Array [Difficulty: Easy]

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  8. 338. Counting Bits

    https://leetcode.com/problems/counting-bits/ 给定一个非负数n,输出[0,n]区间内所有数的二进制形式中含1的个数 Example: For num = 5 ...

  9. Java [Leetcode 338]Counting Bits

    题目描述: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculat ...

随机推荐

  1. 中国首届CSS开发者大会讲师照片

    中国首届CSS开发者大会讲师照片 Bert Bos Winter 点头猪 灭灭 jaychsu Hax 尤雨溪 一丝 勾三股四 小倩 **

  2. luogu2765 魔术球问题

    发现好像没人来证明贪心啊--那我来写一下它的证明 欲证明:放一个数在已有的柱上(如果可以)总是比新开一个柱更优的 假如已经放了x1..x2....xu..xv..xw.... 现在我要放xx 我有两种 ...

  3. luogu2577 [ZJOI2005]午餐

    dp[i]表示第一队打饭时间i的最优解 #include <algorithm> #include <iostream> #include <cstring> #i ...

  4. Java集合数据类型

    Java集合如Map.Set.List等所有集合只能存放引用类型数据,它们都是存放引用类型数据的容器,不能存放如int.long.float.double等基础类型的数据. 1. 集合存储对象 Jav ...

  5. 关于面试总结-SQL学生表

    前言 每次面试必考SQL,小编这几年一直吃SQ的亏,考题无非就是万年不变学生表,看起来虽然简单,真正写出来,还是有一定难度.于是决定重新整理下关于SQL的面试题,也可以帮助更多的人过SQL这一关. 作 ...

  6. 《Nginx高性能Web服务器详解》

    第1章 Nginx初探 第2章 Nginx服务器的安装部署 第3章 Nginx服务器架构初探 第4章 Nginx服务器的高级设置 第5章 Nginx服务器的Gzip压缩 第6章 Nginx服务器的Re ...

  7. Python全栈开发第二期课表

     day01-python 全栈开发-基础篇                 01 开课介绍 01:55:13 ★  02 开课介绍02 01:28:31 ★  03 开课介绍03 00:22:55 ...

  8. [笔记]BFS算法的python实现

    #!/usr/bin/env python # -*- coding:utf-8 -*- graph = {} graph["you"] = ["alice", ...

  9. javascript学习笔记-数据类型

    一 数据类型 基本类型:undefined,null,boolean,number,string     保存在栈内存中     占用空间固定        变量直接从栈内存中存取的是该值 引用类型: ...

  10. 设计模式(十六)迭代器模式 Iterator

    什么时候需要用到迭代器模式? 有许多中方法,可以把对象堆起来放进一个集合(可以是数组.堆栈.列表.哈希表,等等). 每一种类型的集合,都有各自适用的时机.但是某个时间段,客户端可能希望去遍历这个集合. ...