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. iOS学习笔记——键盘处理

    在网上找到的资料比较零散,这部分学起来感觉也有点空虚,内容就只包括隐藏键盘和键盘高度两部分 隐藏键盘其实就在我学习iOS开发的第一个程序里面已经实践过了,不过当时还懵懵懂懂,现在就了解了是什么一回事, ...

  2. python处理空格脚本

    博客园上传代码时拷贝vs里面的代码不能直接粘贴,否则空格会不符合要求 去掉空格代码 # -*- coding: utf-8 -*- '''打开delSpace.txt文本并删除每行开头的八个空格''' ...

  3. ASP.NET5,MVC 6,Beta 7与VS 2015 RTM的兼容问题

    温馨提示:本文杂而乱,最终不知所云. Visual Studio 2015 RTM已经于2015年7月20号正式发布,我也在第一时间下载安装了起来. 虽然在5月份就开始使用RC版本,但是还是很期待正式 ...

  4. 【nodejs笔记——小知识点汇总】

    1.  ejs标签: <%  %> ,  <%-  %> ,  <%= %>的区别 ejs的标签分为三种: (1)<% code %>   javasc ...

  5. js promise chain

    新的标准里增加了原生的Promise. 这里只讨论链式使用的情况,思考一下其中的细节部分. 一,关于 then() 和 catch() 的复习 then() 和 catch() 的参数里可以放置 ca ...

  6. MyEclipse10查看Struts2源码及Javadoc文档

    1:查看Struts2源码 (1):Referenced Libraries >struts2-core-2.1.6.jar>右击>properties. (2):Java Sour ...

  7. [ASP.NET Core] Static File Middleware

    前言 本篇文章介绍ASP.NET Core里,用来处理静态档案的Middleware,为自己留个纪录也希望能帮助到有需要的开发人员. ASP.NET Core官网 结构 一个Web站台最基本的功能,就 ...

  8. Android开发4: Notification编程基础、Broadcast的使用及其静态注册、动态注册方式

    前言 啦啦啦~(博主每次开篇都要卖个萌,大家是不是都厌倦了呢~) 本篇博文希望帮助大家掌握 Broadcast 编程基础,实现动态注册 Broadcast 和静态注册 Broadcast 的方式以及学 ...

  9. SharePoint服务器端对象模型 之 使用CAML进展数据查询

    SharePoint服务器端对象模型 之 使用CAML进行数据查询 一.概述 在SharePoint的开发应用中,查询是非常常用的一种手段,根据某些筛选.排序条件,获得某个列表或者某一些列表中相应的列 ...

  10. 支付宝alipay使用小结 调用支付宝程序被杀死说明

    一. 准备阶段 如果没有蚂蚁金服开放平台的注册账号,则需要实现注册一个,这里多说一点,就是当我们以公司名义注册账号时,需要预备公司的营业执照等物品(需要上传照片等信息审核).账号申请成功之后,我们需要 ...