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. jquery删除添加输入文本框

    效果体验:http://hovertree.com/texiao/jquery/67/ 效果图: 参考:http://hovertree.com/h/bjaf/traversing_each.htm ...

  2. es6 中增强的对象字面量

    http://www.cnblogs.com/Wayou/p/es6_new_features.html 对象字面量被增强了,写法更加简洁与灵活,同时在定义对象的时候能够做的事情更多了.具体表现在: ...

  3. iPhone开发与cocos2d 经验谈

    转CSDN jilongliang : 首先,对于一个完全没有mac开发经验,甚至从没摸过苹果系统的开发人员来说,首先就是要熟悉apple的那一套开发框架(含开发环境IDE.开发框架uikit,还有开 ...

  4. Unsupported major.minor version 52.0问题的解决

    下载Tomcat9.0,解压后安装运行,结果启动失败,进入logs文件夹看里面的日志文件,提示是Unsupported major.minor version 52.0错误,这是因为Tomcat版本过 ...

  5. UDS(ISO14229-2006) 汉译(No.0 前言)

    UDS protocol 前言 ISO(国际标准化组织)是国际标准机构(ISO成员体)的世界性联合会.国际标准的拟定工作通常由ISO技术委员会负责.为每一个主题而建立的技术委员会由对其感兴趣的成员机构 ...

  6. spring-stutrs求解答

    这里贴上applicationContext里的代码: <?xml version="1.0" encoding="UTF-8"?> <bea ...

  7. ahjesus HttpQuery

    /// <summary> /// 有关HTTP请求的辅助类 /// </summary> public class HttpQuery { private static re ...

  8. mysql基本操作

    1.创建表:create table if not exists student(id integer(4) primary key auto_increment,name varchar(10),s ...

  9. 理解 OpenStack 高可用(HA)(5):RabbitMQ HA

    本系列会分析OpenStack 的高可用性(HA)概念和解决方案: (1)OpenStack 高可用方案概述 (2)Neutron L3 Agent HA - VRRP (虚拟路由冗余协议) (3)N ...

  10. 工厂方法(Factory Method),思考

    最近看见一个关于如何通过工厂方法来重构代码的提问,发现这方面,自己还没有想得特别明白,所以,稍作总结. 只要有构造的地方,就有是用工厂方法的可能. 如果考虑到单元测试和实现的扩展,就有是用工厂方法的必 ...