给定一个非负整数 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. 洛谷 1821 [USACO07FEB]银牛派对Silver Cow Party

    [题解] 其实解法 #include<cstdio> #include<cstring> #include<algorithm> #define LL long l ...

  2. xe的debug怪现象

    死活有问题,而且不能重新编译生成文件. 查网上说明:在删除项目xxx.dproj文件后,然后打开dpk文件,会自动生成.dproj文件,再然后一切OK. 的确如此,但莫名其妙.

  3. 从“菜鸟”码农到“资深”架构师,我到底经历了什么?--------http://baijiahao.baidu.com/s?id=1585813883835208757&wfr=spider&for=pc

    http://baijiahao.baidu.com/s?id=1585813883835208757&wfr=spider&for=pc

  4. [luoguP1134] 阶乘问题(数论)

    传送门 我直接用 long long 暴力,居然过了 ——代码 #include <cstdio> int n; long long x, ans = 1; int main() { in ...

  5. HDU 4903 (模拟+贪心)

    Fighting the Landlords Problem Description Fighting the Landlords is a card game which has been a he ...

  6. node.js 发布订阅模式

    //导入内置模块 let EventEmitter = require('events'); let util=require('util'); //Man继承EventEmitter util.in ...

  7. codevs4343 找回密码

    题目描述 Description jrMz 很喜欢动漫<叛逆的鲁鲁修>(额= =不知道是不是因为他盯上了动画片里的 MM),他准备以一种神奇的方式降临<叛逆的鲁鲁修>世界,所以 ...

  8. 如何高效读写百万级的Excel?

    高效读取百万级数据 接上一篇介绍的高效写文件之后,最近抽时间研究了下Excel文件的读取.概括来讲,poi读取excel有两种方式:用户模式和事件模式. 然而很多业务场景中的读取Excel仍然采用用户 ...

  9. [bzoj1613][Usaco2008 Jan]Running贝茜的晨练计划_动态规划

    Running贝茜的晨练计划 bzoj-1613 Usaco-2008 Jan 题目大意:题目链接(U组题题意真的是没法概括qwq....). 注释:略. 想法:一眼dp题. 状态:dp[i][j]表 ...

  10. 模拟赛 Problem 2 不等数列(num.cpp/c/pas)

    Problem 2 不等数列(num.cpp/c/pas) [题目描述] 将1到n任意排列,然后在排列的每两个数之间根据他们的大小关系插入“>”和“<”.问在所有排列中,有多少个排列恰好有 ...