338. 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 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题目详解的更多相关文章
- LN : leetcode 338 Counting Bits
lc 338 Counting Bits 338 Counting Bits Given a non negative integer number num. For every numbers i ...
- 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 ...
- RHCE脚本题目详解
目录 RHCE脚本题目详解 题目一 shell脚本之if语句实现: shell脚本之case语句实现: 题目二 实现 测试 解析 写在后面 RHCE脚本题目详解 题目一 在system1上创建一个名为 ...
- 【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 ...
- Leetcode 338. Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
- 338. Counting Bits
https://leetcode.com/problems/counting-bits/ 给定一个非负数n,输出[0,n]区间内所有数的二进制形式中含1的个数 Example: For num = 5 ...
- Java [Leetcode 338]Counting Bits
题目描述: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculat ...
- js几个经典的题目详解
直接看题目,先不要急着看答案 先自己思考,收获更多 一 var out = 25, inner = { out: 20, func: function () { var out = 30; retur ...
- 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 ...
随机推荐
- [CISCN2019 华东南赛区]Double Secret
0x01 进入页面如下 提示我们寻找secret,再加上题目的提示,猜测这里有secret页面,我们尝试访问,结果如下 根据它这个话的意思,是让我们传参,然后它会给你加密,我们试一下 发现输入的1变成 ...
- 图灵学院笔记-java虚拟机底层原理
Table of Contents generated with DocToc 一.java虚拟机概述 二.栈内存解析 2.1 概述 2.2 栈帧内部结构 2.2.1 我们来解析一下compute() ...
- StringBuffer类和StringBuilder类
StringBuffer类和StringBuilder类 三者比较 String 不可变字符序列 底层用char[]存储 StringBuffer 可变的字符序列 线程安全的 效率低 底层结构使用ch ...
- 题解 洛谷 P4336 【[SHOI2016]黑暗前的幻想乡】
生成树计数的问题用矩阵树定理解决. 考虑如何解决去重的问题,也就是如何保证每个公司都修建一条道路. 用容斥来解决,为方便起见,我处理时先将\(n\)减了1. 设\(f(n)\)为用\(n\)个公司,且 ...
- Linux内存参数
用free -m查看的结果:# free -m total used free shared buffers cachedMem: 50 ...
- IntelliJ IDEA 2019.3.4永久破解(持续更新)--已更新
第一步,下载最新破解包: 链接: https://pan.baidu.com/s/1djUF9TiNZC4rIfxczxfIew 提取码: f521 把破解包两个文件放进bin目录下,这一步极为重要! ...
- Day15_redis安装及配置
学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"乐优商城"获取视频和教程资料! b站在线视频 redi ...
- SUM and COUNT -- SQLZOO
SUM and COUNT 注意:where语句中对表示条件的需要用单引号, 下面的译文使用的是有道翻译如有不正确,请直接投诉有道 01.Show the total population of th ...
- Python File seek() 方法
概述 seek() 方法用于移动文件读取指针到指定位置.高佣联盟 www.cgewang.com 语法 seek() 方法语法如下: fileObject.seek(offset[, whence]) ...
- 4.24 省选模拟赛 欧珀瑞特 主席树 可持久化trie树
很容易的一道题目.大概.不过我空间计算失误MLE了 我草草的计算了一下没想到GG了. 关键的是 我学了一个dalao的空间回收的方法 但是弄巧成拙了. 题目没有明确指出 在任意时刻数组长度为有限制什么 ...