给定一个非负整数 num。 对于范围 0 ≤ i ≤ num 中的每个数字 i ,计算其二进制数中的1的数目并将它们作为数组返回。
示例:
比如给定 num = 5 ,应该返回 [0,1,1,2,1,2].
进阶:
    给出时间复杂度为O(n * sizeof(integer)) 的解答非常容易。 但是你可以在线性时间O(n)内用一次遍历做到吗?
    要求算法的空间复杂度为O(n)。
    你能进一步完善解法吗? 在c ++或任何其他语言中不使用任何内置函数(如c++里的 __builtin_popcount)来执行此操作。
详见:https://leetcode.com/problems/counting-bits/description/

C++:

class Solution {
public:
vector<int> countBits(int num) {
vector<int> res(num+1,0);
for(int i=1;i<=num;++i)
{
res[i]=res[i&(i-1)]+1;
}
return res;
}
};

参考:https://www.cnblogs.com/grandyang/p/5294255.html

338 Counting Bits Bit位计数的更多相关文章

  1. 338. Counting Bits_比特位计数_简单动态规划

    https://leetcode.com/problems/counting-bits/ 这是初步了解动态规划后做的第一道题,体验还不错... 看完题目要求后,写出前10个数的二进制数,发现了以下规律 ...

  2. LN : leetcode 338 Counting Bits

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

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

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

  5. 338. Counting Bits(动态规划)

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

  6. 338. Counting Bits题目详解

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

  7. 338. Counting Bits

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

  8. Java [Leetcode 338]Counting Bits

    题目描述: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculat ...

  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. java属性的默认值

    String 默认null Boolean默认false int默认0 double默认0.0 类中使用自定义类定义属性默认值:null 在定义属性的时候可以指定默认值

  2. HDU 482 String

    String Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 482 ...

  3. rm -rf & node

    rm -rf & node rm -rf $ rm -rf mydir https://www.computerhope.com/issues/ch000798.htm https://sta ...

  4. JQuery中如何重置(reset)表单(且清空隐藏域)

    由于JQuery中,提交表单是像下面这样的: 所以,想当然的认为,重置表单,当然就是像下面这样子喽: 但是,不幸的是,这样写的话,会有一个让你很郁闷的结果,那就是,表单无法重置! 后来,上网查了一下, ...

  5. 设置Linux使用SMTP服务发送邮件

    很多时候我们需要知道服务器的运行状态,比如发生了异常的报警.数据库备份的状态等,假如服务器自动跟你汇报那就好了,我们可以通过设置当触发某些条件时让服务器发送邮件给你,这样你就可以了解你的服务器的状态怎 ...

  6. [codevs 1482]路线统计(矩阵乘法)

    题目:http://codevs.cn/problem/1482/ 分析:很像“经过K条边的最短路径条数”.但有所不同,那就是不是边数固定,而是路径总长度固定.看似不能用矩阵乘法了……但注意到每条边的 ...

  7. oracle 12c show con_name

    今天安装了一个oracle 12c的数据库做测试,在运行一个很简单的命令时出错了: SQL> show con_name concat "." (hex 2e) SP2: u ...

  8. 扩展VirtualBox中的centos硬盘大小

    一.克隆文件 我之前安装的时候建的是centos 6.3.可是后来空间不够,没办法,又不想重装centos.由于好多东西要配置,特麻烦,所以先想到了使用resize命令,可是在win8中运行D:\Pr ...

  9. ViewPager中View的复用

    代码例如以下: public class MyViewPagerAdapter extends PagerAdapter { //显示的数据 private List<DataBean> ...

  10. iOS 设置启动页面 时间

    [NSThread sleepForTimeInterval:3.0];  时间越大  ,启动页面停留的时间越长 iOS 8之后,,创建项目自带的有  LaunchScreen.xib  可直接用