https://leetcode.com/problems/counting-bits/

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:

1、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?
2、Space complexity should be O(n).
3、Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.

方案一,这个效率还不够高

/*** Return an array of size *returnSize.* Note: The returned array must be malloced, assume caller calls free().*/
int* countBits(int num, int* returnSize) {int i;unsigned flag;*returnSize = num + 1;int *a = (int *)malloc((*returnSize) * sizeof(int));a[0] = 0;flag = 0;for(i = 1; i <= num; ++i) {flag = i;a[i] = 0;while(flag > 0) {flag = flag & (flag - 1);a[i]++;}}return a;
}

Counting Bits相关推荐

  1. LeetCode Counting Bits(动态规划)

    问题:给出数字n,求0-n这些数的1的位数. 思路:方法一使用x&(x-1)统计数字1的位数. 方法二[0,1) [2,3)表示形式为[10,11) [4,8)表示形式为[100,101, 1 ...

  2. [刷题]Counting Bits

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

  3. 338. Counting Bits

    为什么80%的码农都做不了架构师?>>>    想法:用一个掩码获取除最高位之外的剩下的几位 其中掩码分别是 0,2-1,4-1,8-1,16-1,... 当mask = 7,x = ...

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

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

  5. Leet Code OJ 338. Counting Bits [Difficulty: Medium]

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

  6. leetcode-- 338. Counting Bits

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

  7. 29/100. Counting Bits

    给定一个非负整数num.对于0≤i≤num范围内的每个数字i,计算其二进制表示形式中的1的个数,并将其作为数组返回. class Solution(object):def countBits(self ...

  8. Counting Bits(Difficulty: Medium)

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

  9. leetcode 上的Counting Bits 总结

    最近准备刷 leetcode  做到了一个关于位运算的题记下方法 int cunt = 0; while(temp) { temp = temp&(temp - 1);  //把二进制最左边那 ...

最新文章

  1. zoj2760(最大流)
  2. 火锅店市场容量或变化趋势_2020-2026年中国氮肥市场深度研究与投资前景预测报告...
  3. 第6章 RTX 操作系统源码方式移植
  4. 用tomcat 发布mule 服务 (转)
  5. 【HDU - 1452】 Happy 2004(因子和,逆元,快速幂)
  6. 职中心得体会300字高一计算机,职业高中毕业的自我鉴定范文300字
  7. recvfrom 无法接收 icmp 差错数据包_利用ICMP隧道技术实现C2通信
  8. 第二章 寄存器基础概念
  9. 开年福利!OpenMMLab 全新企划,等你来
  10. 从零开始编写深度学习库(一)SoftmaxWithLoss CPU编写
  11. 【kafka】The group member needs to have a valid member id before actually entering a consumer group
  12. 如何在矩池云GPU云中安装MATLAB 2019b软件
  13. windbg调试句柄泄露
  14. python爬虫:bs4搜索文档树
  15. 在一个成熟行业里怎么生存下去?
  16. FH153C6常用一键开关机芯片 ON/OFF单键开关IC 美容仪电子开关IC
  17. 如何用Redis统计UV(独立访客)
  18. 又是一江春水自东流!
  19. 富格林金业:原油天然气怎么掌控买卖点?
  20. 24 个很酷的 Linux/Unix 工具

热门文章

  1. 2011年2月--2011年7月数据库性能优化过程
  2. 3d geometric model website http://www.cse.ohio-state.edu/~tamaldey/
  3. 我的世界服务器物品展示框,我的世界怎么展示框物品 展示框攻略
  4. python中hashset_python中的集合
  5. struts2从一个action跳到另一个action的配置方法
  6. mysql affected rows_mysql_affected_rows函数定义与用法汇总
  7. android tab 点击,TabLayout.Tab点击事件
  8. 存储1m图像错误_【1m 讲堂】“跨专业挑战空调施工常见错误,你敢来参加吗?”...
  9. java 图片压缩100k_如何将图片压缩到100K以内,教你几种免费方法
  10. php查询oracle大量数据,Oracle 百万行数据查询优化