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.

借助之前算过的位数来计算,假如ret中保存结果,i的二进制有n位,则ret[i]表示i中bit为1的位数,ret[i >> 1]表示去掉i中最低位的结果,ret[i>>1] + (i&1)即为i的前n-1位为1的数量加上自己最后一位是否为1

class Solution {
public:
vector<int> countBits(int num) {
vector<int> ret;
ret.push_back(0);
for(int i = 1; i <= num; i++)
{
int bits = ret[i >> 1] + (i & 1);
ret.push_back(bits);
}
return ret;
}
};

LeetCode 第 338 题 (Counting Bits)的更多相关文章

  1. 【Leetcode 338】 Counting Bits

    问题描述:给出一个非负整数num,对[0, num]范围内每个数都计算它的二进制表示中1的个数 Example:For num = 5 you should return [0,1,1,2,1,2] ...

  2. 【leetcode】经典算法题-Counting Bits

    题目描述: 给定一个数字n,统计0-n之间的数字二进制的1的个数,并用数组输出 例子: For num = 5 you should return [0,1,1,2,1,2]. 要求: 算法复杂复o( ...

  3. [Leetcode] 第338题 比特位计数

    一.题目描述 给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回. 示例 1: 输入: 2 输出: [0,1,1] 示例 ...

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

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

  5. LN : leetcode 338 Counting Bits

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

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

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

  8. Leetcode重点 250题-前400 题

    删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & ...

  9. LeetCode 第 342 题(Power of Four)

    LeetCode 第 342 题(Power of Four) Given an integer (signed 32 bits), write a function to check whether ...

随机推荐

  1. Popupwindow全屏问题

    //sdk > 21 解决 标题栏没有办法遮罩的问题 popupWindow.setClippingEnabled(false);

  2. free -m 下的含义

    如下显示free是显示的当前内存的使用,-m的意思是M字节来显示内容.我们来一起看看. $ free -mtotal used free shared buffers cachedMem: 1002 ...

  3. thinkphp3.2 实现留言功能

    写一个例子说明一下: 前端:http://www.mmkb.com/zhendao/index/feedback.html <form method="post" actio ...

  4. redis基本结构

    redis数据结构string(字符串)1->12->23->3 list(列表)key->(1->2->3->4->5) hash(散列)1-> ...

  5. 【PHP】使用GD库实现 图像生成、缩放、logo水印和简单验证码

    gd库是php最常用的图片处理库之一(另外一个是imagemagick),可以生成图片.验证码.水印.缩略图等等.要使用gd库首先需要开启gd库扩展, windows系统下需要在php.ini中将ex ...

  6. LESS CSS 框架简介与使用

    简介 CSS(层叠样式表)是一门历史悠久的标记性语言,同 HTML 一道,被广泛应用于万维网(World Wide Web)中.HTML 主要负责文档结构的定义,CSS 负责文档表现形式或样式的定义. ...

  7. jquery.gritter.js简介

    Gritter 是一个小型的 jQuery 消息通知插件,通知效果如下图所示: 参考网

  8. smarty直接在模板中格式化时间的方法

    smarty提供了一个获取时间戳的方法: <%$smarty.now%> 使用该方法获取到当时的时间戳之后,使用格式化修饰符data-format进行修饰: <%$smarty.no ...

  9. 使用TELNET手工操作 IMAP 查看邮件

    http://www.cnblogs.com/CrazyWill/archive/2006/08/12/474884.html IMAP 协议收信与POP收信有很大的不同,最明显的一点就是发送的每条命 ...

  10. eclipse安装maven时候如果conf文件夹中有setting文件则会以这个文件为主,如果自己设置了user的配置文件则会无效

    eclipse安装maven时候如果conf文件夹中有setting文件则会以这个文件为主,如果自己设置了user的配置文件则会无效