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 ...
随机推荐
- IE11 CSS hack
IE11 怎么做 CSS hack ? 很简单. @media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) { ...
- wordpress学习笔记
版本:4.9.8 我用wordpress的初衷是借用它的后台系统,前端用自己的网页显示存在wordpress数据库里的文章. wordpress本质上是个框架,技术栈:web-php-mysql. 初 ...
- jsp课堂笔记5 Java servlet
servlet的创建 servlet类就是一个包含javax.servlet.http包中的HttpServlet类 部署web.xml文件 在<sevlet>标签中创建 <serv ...
- MySQL主从分离实现
前言 大型网站为了减轻服务器处理海量的并发访问,所产生的性能问题,采用了很多解决方案,其中最主流的解决方案就是读写分离,即将读操作和写操作分别导流到不同的服务器集群执行,到了数据业务层,数据访问层 ...
- 下载spring的路径的文章,已经试用没问题
文章:https://blog.csdn.net/ethan__xu/article/details/80273249 spring jar下载路径 http://repo.spring.io/rel ...
- Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform!
原文链接:https://blog.csdn.net/u012700515/article/details/56009429 Maven 打包时有标题中警告,需要在pom.xml文件中添加 <p ...
- map,reduce,filter基础实现
#coding=gbk from operator import add # 导入加法 # map 函数名 , 序列对象 print(list(map(str,range(5)))) print(li ...
- 图文并茂,带你认识 JVM 运行时数据区
跨平台的本质 关于 JVM, Java 程序员的最熟悉的一句话就是:一处编码,到处执行,指的就是 Java 语言可以通过 JVM 实现跨平台.而跨平台到底跨越了什么这个问题相信很少有人知道,接下来就跟 ...
- PHP date_get_last_errors() 函数
------------恢复内容开始------------ 实例 返回解析日期字符串时的警告和错误: <?phpdate_create("gyuiyiuyui%&&/ ...
- C/C++编程笔记:C语言贪吃蛇源代码控制台(一),会动的那种哦!
前几天有个同学加我QQ私聊我说他们老师布置了一个贪吃蛇,他不知道怎么写所以来找我求解,我给他简单讲解了思路和一些难点之后他也能够自己独立将项目完成了!考虑到更多同学可能有贪吃蛇上的问题,今天有时间就来 ...