题目详情

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 1:

Input: 2
Output: [0,1,1]

Example 2:

Input: 5
Output: [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.

分析

解法1 —— 直接求解

当然最简单的做法就是遍历1-n,然后每个数字直接求其1的数字,这样复杂度变成了O(nlogn)

这里就不给出具体代码了

解法2 —— 动态规划问题

我们可以把求某个数的1的个数,写出一个递推式

定义re 为返回的数组, 则re[i] 就表示 i的二进制表示中的1的个数,我们把i的二进制表示分成两段,一段为前n - 1位, 另一段为 最右边一位。

所以得到 re[i] = 前n-1位中的1的个数 + i%2

而 前n-1位中1的个数可以用re数组的一个元素来表示, 也就是re[i >> 1]

所以得到递归式子

re[i] = re[i>>1] + i%2;

然后循环 把re的所有项都得到。解法1之所以复杂度高是因为每次算1的个数的时候重复计算了前面的1的个数, 而动态规划里把前面数字的1的个数都记录下来,避免了重复计算, 但是这也要求需要一个数组来存储结果, 这是通过牺牲空间来换取时间的一种策略

代码如下:

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

解法3

这是最快的一种解法

/*
Dynamic programming
Reoccurence relation:
dp[i] = dp[i & (i - 1)]] + 1 where i & (i - 1) erases right most bit
i & -i gives rightmost bit => i - (i & -i) erases rightmost bit
O(n)
*/ class Solution {
public:
vector<int> countBits(int num) {
vector<int> dp(num + 1, 0); for (int i = 1; i < num + 1; ++i) {
dp[i] = dp[i & (i - 1)] + 1;
} return dp;
}
};

338. Counting Bits题目详解的更多相关文章

  1. LN : leetcode 338 Counting Bits

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

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

  3. RHCE脚本题目详解

    目录 RHCE脚本题目详解 题目一 shell脚本之if语句实现: shell脚本之case语句实现: 题目二 实现 测试 解析 写在后面 RHCE脚本题目详解 题目一 在system1上创建一个名为 ...

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

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

  7. Java [Leetcode 338]Counting Bits

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

  8. js几个经典的题目详解

    直接看题目,先不要急着看答案 先自己思考,收获更多 一 var out = 25, inner = { out: 20, func: function () { var out = 30; retur ...

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

随机推荐

  1. 【软件安装】CentOS7_直播服务搭建_nginx_nginx-http-flv-module

    1.介绍 nginx-http-flv-module是在nginx-rtmp-module基础上开发的一个直播模块. 感谢Arut创造了nginx-rtmp-module,它是Nginx的一个优秀的第 ...

  2. git配置httpd服务-web_dav模式

    1,搭建httpd应用 2,修改httpd.conf文件 注释 DocumentRoot "/data/httpd/htdocs" 注释 <Directory "/ ...

  3. C++语法小记---类型检测

    类型检测 C++使用typeid关键字进行类型检查 不同的编译器使用typeid返回的类型名称不严格一致,需要特别注意 也可以使用虚函数,返回各自的类型名 如果typeid的操作数不是类类型(类指针也 ...

  4. Redis Desktop Manager安装

    Windows安装: 1.下载安装包 官网下载地址:https://redisdesktop.com/pricing 官网下载需要付费使用 再此附上一个免费的破解版本,绿色安全可用 链接:https: ...

  5. Docker 概念-2

    Docker 是什么? 说了这么多, Docker 到底是个什么东西呢?我们在理解 Docker 之前,首先得先区分清楚两个概念,容器和虚拟机. 可能很多读者朋友都用过虚拟机,而对容器这个概念比较的陌 ...

  6. Android:自定义BaseActivity基类

    使用BaseActivity可以封装一些重复代码例如设置标题栏颜色,封装一些工具类... 主要功能: 封装Toast 新建一个BaseActivity继承自Activity package com.o ...

  7. onsubmit校验表单时利用ajax的return false无效解决方法-转

    原来的代码 function checkNewEmail(){ var re_email=new RegExp("\\w+@\\w+\\.\\w+\\.?\\w*");      ...

  8. JavaScript高级程序设计(第三版) 6/25

    第六章面向对象的程序设计 1.定义只有在内部才用的特性(attribute)时,描述了属性(property)的各种特征.这些特性是为了实现JavaScript引擎用的,因此在JavaScript中不 ...

  9. Ubuntu安装Cloudera Manager以及CDH5.15.2

    一.机子分配 注意,本安装教程是在真机上进行,而非虚拟机.另,此次搭建主要的目的是搭建测试环境,让Hadoop各组件能够运作起来即可,完成搭建后,将用小数据量进行相关数据的计算与测试.线上环境将会使用 ...

  10. Metal 线宽如何选择

    https://www.cnblogs.com/yeungchie/ Metal 线宽如何选择 假如Metal是为了传输电流,则主要需要从解决和减小它的寄生电阻.寄生电容方面多做考虑.寄生电感一般忽略 ...