Counting Bits -leetcode
introduction:
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.
class Solution {
public:
vector<int> countBits(int num) {
vector<int> result;
if(num>=) // 1. not num==0
result.push_back();
if(num>=) // 1. not num==1
result.push_back();
for (int i=;i<=num;i++)
{
int k = (log(i)/log());
if(i>=pow(,k) && i<pow(,k)+pow(,k-)) // 2. use pow (in math.h)
result.push_back(result.at(i-pow(,k-)));
else if(i>=pow(,k)+pow(,k-) && i<pow(,k+)) // 3. >= or >
result.push_back(result.at(i-pow(,k-))+);
}
result;
}
};
解题思路:从 1 到15 ,它的1的个数可以在二叉树中看出(从左到右,从上到下,15=b1111为最后一个对应4)。
而且 满足 : 第k+1层的前半部分 是第k层的复制 , 后半部分是第k层所有元素加1
debug:
1、 如果 是 ==0 或者 ==1 作为判断,当>1 时,将会缺少0和1的pushback
2、 幂运算 result=pow(x,y);
3、 注意判断语句的边界条件
注: 本文原创,转载请说明出处
Counting Bits -leetcode的更多相关文章
- Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits)
Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits) 给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数 ...
- 【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 ...
- 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 ...
- LeetCode Counting Bits
原题链接在这里:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. Fo ...
- [LeetCode] Counting Bits 计数位
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
- leetcode 上的Counting Bits 总结
最近准备刷 leetcode 做到了一个关于位运算的题记下方法 int cunt = 0; while(temp) { temp = temp&(temp - 1); //把二进制最左边那 ...
- leetcode 338. Counting Bits,剑指offer二进制中1的个数
leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种 ...
- LeetCode 第 338 题 (Counting Bits)
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
随机推荐
- 编译可供C#调用的C/C++动态链接库dll文件
编译可供C#调用的C/C++动态链接库dll文件,C语言控制台应用程序,探索生成dll过程 由于项目需求,需要公司另一个团队提供相关算法支持,是用C语言编译好的dll库提供给我们进行调用. 但是拿到d ...
- ubuntu 12.04 LTS 如何使用更快的更新源
装好ubuntu系统后的第一见事就是替换自带的更新源,原因是系统自带的源有些在中国访问不了,可以访问的速度又特别慢.幸好国内的一些公司和大学提供了速度不错的更新源.下面介绍如何使用更快的更新源 方法/ ...
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
原因分析 首先我们来看下Spring mvc 中文件上传的配置 <bean id="multipartResolver" class="org.springfram ...
- python基础之文件处理
读和写文件 读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直 ...
- 从头开始搭建一个mybatis+postgresql平台
最近有个项目的数据库使用postgresql,使用原生态的mybatis操作数据,原生态的没什么不好,只不过国内有个tk.mybatis的工具帮助我们做了很多实用的事情,大多数情况下我们需要 ...
- iOS导航栏标题颜色
按钮的颜色 [self.navigationBar setTintColor:[UIColor whiteColor]]; 标题颜色.字体 [self.navigationBar setTitleTe ...
- OC的runtime运行机制
什么是runtime runtime就是一套底层的c语言API(Application Programming Interface)里面包括很多强大实用的c语言类型.c语言函数. 实际上,平时我们编写 ...
- spring-boot-framework 如何自动将对象返回成json格式
使用srping-rest-mvc 的时候只要在工程的classpath中包含jackson的2.x版本,就可以不用自己做json格式的转换了. 如在你的pom文件中加入以下的依赖: <depe ...
- log4j配置
log4j.rootCategory=DEBUG , R, D,stdout # Console log4j.appender.stdout=org.apache.log4j.ConsoleAppen ...
- iOS 实现转盘的效果
效果 #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBO ...