题目:

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. Java做爬虫也很方便

    首先我们封装一个Http请求的工具类,用HttpURLConnection实现,也可以用HttpClient, 或者直接用Jsoup来请求. 工具类实现比较简单,就一个get方法,读取请求地址的响应内 ...

  2. [转]mysql Access denied for user 'root'@'localhost' 问题的解决方法

    解决方案如下: # /etc/init.d/mysql stop # mysqld_safe --user=mysql --skip-grant-tables --skip-networking &a ...

  3. day04_07 while循环01

    while循环结构: #while 条件: print("any") print("any") 死循环案例 num = 1 while num<=10 : ...

  4. Wannafly模拟赛2

    Contest 时间限制:1秒 空间限制:131072K 题目描述 n支队伍一共参加了三场比赛. 一支队伍x认为自己比另一支队伍y强当且仅当x在至少一场比赛中比y的排名高. 求有多少组(x,y),使得 ...

  5. [错误处理]python大小写敏感,关键字不要写错

    今天调试程序,发现了一个极为隐蔽的bug. True False关键字大小写写错了,然后半天没找出问题所在.

  6. ibatis 动态SQL

    直接使用JDBC一个非常普遍的问题就是动态SQL.使用参数值.参数本身和数据列都是动态SQL,通常是非常困难的.典型的解决办法就是用上一堆的IF-ELSE条件语句和一连串的字符串连接.对于这个问题,I ...

  7. curl 设置头部

    2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ...

  8. Tarjan的强联通分量

    求强联通分量有很多种. <C++信息学奥赛一本通>  中讲过一个dfs求强联通分量的算法Kosdaraju,为了骗字数我就待会简单的说说.然而我们这篇文章的主体是Tarjan,所以我肯定说 ...

  9. 阿狸的打字机(bzoj 2434)

    Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的 ...

  10. SharepPoint 2013安装体会

    SharePoint 2013出来了,一直没有找到合适的机器来安装.前天腾出来一台内存8G的机器,决定在Hyper-V上安装在一台虚机,然后装个Windows 2012,再装SharePoint 20 ...