introduction:

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.
 class Solution {
public:
vector<int> countBits(int num) {
vector<int> result;
if(num>=) // 1. not num==0
result.push_back();
if(num>=) // 1. not num==1
result.push_back();
for (int i=;i<=num;i++)
{
int k = (log(i)/log());
if(i>=pow(,k) && i<pow(,k)+pow(,k-)) // 2. use pow (in math.h)
result.push_back(result.at(i-pow(,k-)));
else if(i>=pow(,k)+pow(,k-) && i<pow(,k+)) // 3. >= or >
result.push_back(result.at(i-pow(,k-))+);
}
 result; 
  }
};

解题思路:从 1 到15 ,它的1的个数可以在二叉树中看出(从左到右,从上到下,15=b1111为最后一个对应4)。

而且 满足 : 第k+1层的前半部分 是第k层的复制 , 后半部分是第k层所有元素加1

debug:

  1、 如果 是 ==0 或者  ==1 作为判断,当>1 时,将会缺少0和1的pushback

  2、 幂运算  result=pow(x,y);

  3、 注意判断语句的边界条件

注: 本文原创,转载请说明出处

Counting Bits -leetcode的更多相关文章

  1. Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits)

    Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits) 给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数 ...

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

  3. LN : leetcode 338 Counting Bits

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

  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 Counting Bits

    原题链接在这里:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. Fo ...

  6. [LeetCode] Counting Bits 计数位

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

  7. leetcode 上的Counting Bits 总结

    最近准备刷 leetcode  做到了一个关于位运算的题记下方法 int cunt = 0; while(temp) { temp = temp&(temp - 1);  //把二进制最左边那 ...

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

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

  9. LeetCode 第 338 题 (Counting Bits)

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

随机推荐

  1. C# 特性学习之一、CallerMemberName、CallerFilePath和CallerLineNumber

    在开发中经常会写个公有静态类记录日志,如下: /// <summary> /// Writes the error. /// </summary> /// <param ...

  2. 谈一谈.net析构函数对垃圾回收的影响

    之前忘了说了 代码都是在Release模式下运行的,现在补充上. 这里说析构函数,其实并不准确,应该叫Finalize函数,Finalize函数形式上和c++的析构函数很像 ,都是(~ClassNam ...

  3. EC笔记:第二部分:12、复制对象时勿忘其每一个成分

    EC笔记:第二部分:12.复制对象时勿忘其每一个成分 1.场景 某些时候,我们不想使用编译器提供的默认拷贝函数(包括拷贝构造函数和赋值运算符),考虑以下类定义: 代码1: class Point{ p ...

  4. 糖果 bzoj 2330

    糖果(1s 128MB)candy [题目描述] 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明 ...

  5. PowerDesigner 15设置mysql主键自动增长及基数

    PowerDesigner 15设置mysql主键自动增长及基数 1.双击标示图,打开table properties->columns,  如图点击图标Customize Columns an ...

  6. 16、ASP.NET MVC入门到精通——MVC过滤器

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 在ASP.NET MVC中有四种过滤器类型

  7. qq空间返回顶部代码

    点击这里体验效果 以下是源代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...

  8. VS2010中整理代码快捷键

    快捷键:Ctrl+A Ctrl+K Ctrl+F的三个的组合2015-11-08

  9. Linux0.11内核--缓冲区机制大致分析

    文件系统的文件太多,而且是照搬的MINIX的文件系统,不想继续分析下去了.缓冲区机制和文件系统密切相关,所以这里就简单分析一下缓冲区机制. buffer.c 程序用于对高速缓冲区(池)进行操作和管理. ...

  10. ios 性能优化策略

    1.尽量不用动态高度 2.如果是动态高度的话,提前计算好即将展示的高度并使用 一定规则跟对应的对象进行绑定缓存起来以便下一次使用 3.不要在layoutSubViews 方法中对UI elements ...