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.

思路:

O(n*n)过于简单,不再赘述。为了实现O(n),需要观察一下。一下描述用c(n)来代表n包含二进制1的个数。

  • c(0) = 0, c(1) = 1;
  • c(2) = 1, c(3) = c(1) + c(2) = c(1) + 1;
  • c(4) = 1, c(5) = c(1) + c(4) = c(1) + 1, c(6) = c(2) + c(4) = c(2) + 1, c(7) = c(3) + c(4) = c(3) + 1;
  • ……
  • c(n) = c(n-m) + 1; m是小于等于n的最大2的整次幂
 1 class Solution {
 2 public:
 3     vector<int> countBits(int num) {
 4         vector<int> vec;
 5         int cur = 1;
 6         int next = 2;
 7         for(int i=0;i<=num;i++) {
 8             if (i < 2)
 9             {
10                 vec.push_back(i);
11                 continue;
12             }
13             if (i == next)
14             {
15                 cur = next;
16                 next = next << 1;
17             }
18             if (i >= cur)
19             {
20                 vec.push_back(vec[i-cur]+1);
21             }
22         }
23         return vec;
24     }
25 };

转载于:https://www.cnblogs.com/wonderday/p/5307524.html

[刷题]Counting Bits相关推荐

  1. Leetcode刷题指南和top100题目

    原文链接:https://blog.csdn.net/qq_39521554/article/details/79160815 参考博文:https://blog.csdn.net/mmc2015/a ...

  2. leetcode刷题规划

    LeetCode精华题目列表[刷题规划系列] – TuringPlanet 目录 算法题到底在考察什么? 题目列表 Array String Linked List Queue Stack Advan ...

  3. ACM比赛经验、刷题记录及模板库总结(更新中)

    前言 本文所提及的部分题目代码,可以在我的Github上找到 第一部分 经验分享及感受 第二部分 刷题记录 一.基础算法&程序语言 //strlen()函数的复杂度是O(n)要小心 //截取字 ...

  4. 力扣(LeetCode)刷题,简单+中等题(第32期)

    目录 第1题:数组的度 第2题:托普利茨矩阵 第3题:爱生气的书店老板 第4题:翻转图像 第5题:有效的数独 第6题:无重复字符的最长子串 第7题:区域和检索 - 数组不可变 第8题:二维区域和检索 ...

  5. 2022/4/3刷题

    2022/4/3刷题 Pascal's triangle(Turn 45 degrees counterclockwise) RESULT Output:Three numbers from big ...

  6. [SHOI2015]自动刷题机

    [SHOI2015]自动刷题机 题目描述 曾经发明了信号增幅仪的发明家 SHTSC 又公开了他的新发明:自动刷题机--一种可以自动 AC 题目的神秘装置. 自动刷题机刷题的方式非常简单:首先会瞬间得出 ...

  7. 算法刷题-数论-试除法求约数、约数个数、约数之和、最大公约数(辗转相除法)

    文章目录 acwing869. 试除法求约数 acwing870. 约数个数 acwing871. 约数之和 acwing872. 最大公约数 acwing869. 试除法求约数 acwing869. ...

  8. $2019$ 暑期刷题记录 $2$(基本算法专题)

    $ 2019 $ 暑期刷题记录 $ 2 $ (基本算法专题) $ by~~wch $ $ BZOJ~1958~Strange~Towers~of~Hanoi $ (动态规划,递推) 题目大意: 求有 ...

  9. [刷题记录] luogu网络流24题 及 网络流心得体会 及 经典模型不定期更新

    文章目录 信息汇总表格 飞行员配对方案问题 分配问题 运输问题 数字梯形问题 最小路径覆盖问题 魔术球问题 圆桌问题 试题库问题 深海机器人问题 航空路线问题 火星探险问题 太空飞行计划问题 方格取数 ...

最新文章

  1. 未能加载文件或程序集Microsoft.VisualStudio.Web.PageInspector.Loader
  2. Swift 学习- 02 -- 基础部分2
  3. URI is not registered (Settings | Languages Frameworks | Schemas and DTDs)
  4. android提交项目到svn,Android Studio 配置SVN,通过Share project提交项目和实现忽略文件...
  5. springboot如何对本地数据库增删改查_SpringBoot 数据库操作 增删改查
  6. 每日一“酷”之string
  7. 计算机技术能力校本培训总结,信息技术校本培训总结
  8. python 爬虫爬取bilibili
  9. Centos7.6安装Bugzilla5.0.4
  10. 【转】Sphinx速成指南
  11. python翻页爬取豆瓣影评,翻页爬取豆瓣电影名称和评分
  12. angular 易犯错误
  13. 机智是一项无价的处世技巧(双语)
  14. 辐射避难所买了东西显示服务器异常,辐射避难所各种BUG怎么解决 所有BUG应对方法一览...
  15. 别把自己当作“千里马”,“笨鸟”先飞才是硬道理。
  16. “图形驱动程序无法找到兼容的图形硬件”的处理方式
  17. 解决挂过代理之后ip不变
  18. 1.3.9 python中进程与线程
  19. 三国志战略版S5赛季事件战法简评
  20. 兄弟连 职业素质改进计划个人总结

热门文章

  1. 主从表 ajax,DWZ主从表结构 · Issue #72 · OtakuFly/dwz · GitHub
  2. Verilog随机数生成$random
  3. Matlab如何从dat或者txt文件读入数据
  4. python读写excel数据--pandas
  5. 什么是Terraform?
  6. 超酷图片压缩工具,就是不支持批量
  7. Beta冲刺随笔集合
  8. XML文档操作工具类
  9. Zend Guard6.0使用教程——PHP代码加密
  10. windows codeblocks clang 3.7.0