题目描述:

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:

    1. You should make use of what you have produced already.
    2. Divide the numbers in ranges like [2-3], [4-7], [8-15] and so on. And try to generate new range from previous.
    3. Or does the odd/even status of the number help you in calculating the number of 1s?

解题思路:

0     0000

1     0001

2     0010

3     0011

4     0100

5     0101

6     0110

7     0111

8     1000

9     1001

10   1010

11   1011

12   1100

13   1101

14   1110

15   1111

........

观察上面的情况,我们发现0,1,2-3,4-7,8-15为一组,且每组开头的1的位数都是1。每组其余的数都可以用本组开头的数加上另一个差值。且这两个数都已经在前面算过了。

代码如下:

public class Solution{
public int[] countBits(int num){
int[] res = new int[num + 1];
int pow = 1, k = 1;
res[0] = 0;
while(k <= num){
if(k == pow){
pow *= 2;
res[k++] = 1;
} else {
res[k] = res[pow / 2 ] + res[k - pow / 2];
k++;
}
}
return res;
}
}

Java [Leetcode 338]Counting Bits的更多相关文章

  1. LN : leetcode 338 Counting Bits

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

  2. leetcode 338. Counting Bits,剑指offer二进制中1的个数

    leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种 ...

  3. Leetcode 338. Counting Bits

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

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

  5. 【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  ...

  6. 【LeetCode】338. Counting Bits 解题报告(Python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目描述 Given a non negati ...

  7. 【LeetCode】Counting Bits(338)

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

  8. 【leetcode】338 .Counting Bits

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

  9. 338. Counting Bits

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

随机推荐

  1. Unity3d 接入 移动MM支付SDK(2.3) 全攻略

    原地址:http://blog.csdn.net/dingxiaowei2013/article/details/26842177 先将例程运行起来 下载例程(csdn积分不够上传不了,只能用百度网盘 ...

  2. HDU 2136 Largest prime factor(查找素数,筛选法)

    题目梗概:求1000000以内任意数的最大质因数是第几个素数,其中 定义 1为第0个,2为第1个,以此类推. #include<string.h> #include<stdio.h& ...

  3. hadoop倒排索引

    1.前言 学习hadoop的童鞋,倒排索引这个算法还是挺重要的.这是以后展开工作的基础.首先,我们来认识下什么是倒拍索引: 倒排索引简单地就是:根据单词,返回它在哪个文件中出现过,而且频率是多少的结果 ...

  4. String类的使用 Part1

    String类的属性 1:Chars属性 获取当前 String 对象中位于指定位置的 Char 对象. 2:Length属性 获取当前 String 对象中的字符数. eg:获取字符串中  字母, ...

  5. lintcode:previous permutation上一个排列

    题目 上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其上一个排列是[4,3,2,1] 注意 ...

  6. 【mongoDB基础篇②】PHP-mongo扩展的编译以及使用

    安装PHP-mongo扩展 安装php-mongo扩展和安装其他php扩展的步骤一样: #1.首先上http://pecl.php.net上面搜索mongo,得到下载地址 wget http://pe ...

  7. 企业用户2T(含秒传),普通用户20G

    周鸿祎一定要看的建议(要求置顶):可以解决本次云盘事件的建议!!! 2016-10-23 20:23 | 复制链接 | 淘帖 461334 本帖最后由 cqthxin 于 2016-10-23 20: ...

  8. VIM Taglist + ctags

    Windows下 进入http://ctags.sourceforge.net/ 下载ctags 把ctags58.zip解压,随便放个地方,我放到了Home\Vim\vim72下,在ctags58文 ...

  9. iOS LLDB调试器

    随着Xcode 5的发布,LLDB调试器已经取代了GDB,成为了Xcode工程中默认的调试器.它与LLVM编译器一起,带给我们更丰富的流程控制和数据检测的调试功能.LLDB为Xcode提供了底层调试环 ...

  10. 转:在MyEclipse下创建Java Web项目 入门(图文并茂)经典教程

    本文是一篇在Myeclipse下构建Java Web项目的初级教程.图文并茂,非常详细.所用的Myeclipse版本是7.5. 第一步:新建Web Project,如下图. 第二步,在弹出的窗口填写下 ...