题目:

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

实现:

 1 class Solution {
 2 public:
 3     vector<int> countBits(int num) {
 4    vector<int> result;
 5     for (int i = 0; i <= num; i++)
 6     {
 7         int count = 0;
 8         int j = i;
 9         while (j)
10         {
11             ++count;
12             j = (j-1) & j;
13         }
14         result.push_back(count);
15     }
16     return result;
17     }
18 };

转载于:https://www.cnblogs.com/lrh-xl/p/5462833.html

Counting Bits(Difficulty: Medium)相关推荐

  1. LeetCode 12 数字转化为罗马符号(难度: Medium)

    题目大意:数字转化为罗马符号 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Sy ...

  2. HDU 6184 2017广西邀请赛:Counting Stars(三元环)

    题意: n个点m条边的无向图,问有多少个A-structure 其中A-structure满足V=(A,B,C,D) && E=(AB,BC,CD,DA,AC) 可以看出A-struc ...

  3. 【Kata Daily 190927】Counting sheep...(数绵羊)

    题目: Consider an array of sheep where some sheep may be missing from their place. We need a function ...

  4. 放俩算法上来吧 (difficulty:easy)

    顺便分享下lintcode 和leetcode 俩刷题网站.个人感觉leetcode舒服一点,虽然 in English 慢慢这个可以写个系列吧,不过慢慢来吧,以后再汇总,先就这样吧,这俩都是EASY ...

  5. LeetCode 从零单刷个人笔记整理(持续更新)

    更新至2020.2.23 github:https://github.com/ChopinXBP/LeetCode-Babel 本人博客用于个人对知识点的记录和巩固. 用几乎所有可行的方法进行了实现和 ...

  6. 嵌入式系统词汇速查表,不会时候拿出来看看!(精品收藏)

    A ASIC(专用集成电路) Application-Specific Integrated Circuit. A piece of custom-designed hardware in a chi ...

  7. WiFi(Wireless Fidelity)基础(五)

    目录 一.基本介绍(Introduction) 二.进化发展(Evolution) 三.PHY帧((PHY Frame ) 四.MAC帧(MAC Frame ) 五.协议(Protocol) 六.安全 ...

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

  9. 2019 ACM - ICPC 上海网络赛 E. Counting Sequences II (指数型生成函数)

    繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量题解和代码,题目难度不一定按照题号排序,我会在每道题后面加上题目难度指数(1∼51 \sim 51∼5),以模板题难度 11 ...

最新文章

  1. 内核:多内核操作模式
  2. dd命令测试linux磁盘io情况,【LINUX】正确的使用dd进行磁盘读写速度测试
  3. 【人脸对齐-Landmarks】人脸关键点检测方法及评测汇总
  4. Linux 硬盘管理
  5. 安全彻底的卸载工具App Cleaner Uninstaller for Mac
  6. python md5加密字符串_Python使用MD5加密字符串示例
  7. Java技术栈(总图)
  8. 融云 即时通讯 集成相关问题
  9. 华为那些手机有鸿蒙系统,华为鸿蒙系统支持哪些手机 鸿蒙系统支持哪些手机型号...
  10. 有机合成化学类毕业论文文献(推荐10篇)
  11. 取消 “是否把IE8设置为默认浏览器提示 提示
  12. git中staged 和 unstaged的区别
  13. 通过构建Paint App学习React Hooks
  14. SonTek -Argonaut-SL500流量计适配调试说明
  15. laravel-excel使用3(老猫包子店的故事)
  16. android仿喜马拉雅APP状态栏,源码分享:高仿喜马拉雅FM
  17. uni-app开发中,使用ThorUI中的图片上传,自定义接口返回数据修改
  18. [二造] 浙江土建实务案例题(索赔、结算、偏差分析)
  19. Lenovo Thinkpad T14电脑 Hackintosh 黑苹果efi引导文件
  20. python抽奖代码_python编写实现抽奖器代码示例

热门文章

  1. 变化的和不变的。。。(2004版)
  2. ubuntu下面markdown转化为word文档
  3. linux终端配备时钟oh-my-zsh主题
  4. flink面试题(需要逐个突破)
  5. hbase shell显示时间与系统时间不同
  6. fatal error: opencv2/core/cvdef.h: 沒有此一檔案或目錄
  7. Ubuntu16.04(64位)下面使用Doxygen+Graphviz分析python代码
  8. mysql 算子 谓词_[SQL] SQL 基础知识梳理(六)- 函数、谓词、CASE 表达式
  9. delphi 调用dll 整形返回值_VS2015 编写C++ DLL库及C++、 C#、python 调用
  10. 测试插件-infinitest介绍