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 ...
随机推荐
- PHP工作笔记:数组转字符串与字符串转数组
一个数组要入库,发现不可以,特定用函数转为字符串入库 $data = array( 'http://img4.bitautoimg.com/autoalbum/files/20110420/734/2 ...
- python之消息队列
引言 你是否遇到过两个(多个)系统间需要通过定时任务来同步某些数据?你是否在为异构系统的不同进程间相互调用.通讯的问题而苦恼.挣扎?如果是,那么恭喜你,消息服务让你可以很轻松地解决这些问题.消息服务擅 ...
- Java编程里类的继承
今天,我们将要讨论的内容是Java里面类的继承的相关概念. 说到继承,我相信大家都不陌生.生活中,子承父业,子女继承父母的财产,这就是继承.实际上,Java里的继承也是如此.对于一个类来说,它的数据成 ...
- 简单介绍JSON
如下图:
- arcgis地图数据集合
一.全国的ArcGIS地图SHP格式数据,覆盖的图层信息量基本齐全,除了ArcGIS之外,其他GIS软件(superMap和MapGIS.MapInfo等等)也是用之,适合为GIS开发提供数据素材. ...
- 【转】Linux 初始化 init 系统 [sysvinit systemd upstart]
http://www.ibm.com/developerworks/cn/views/linux/libraryview.jsp?sort_by=&show_abstract=true& ...
- Android—初识AsyncTask
AsyncTask是用来处理一些后台的比较耗时的任务,给用户带来良好的体验.AsyncTask扩展Thread,增强了与主线程的交互能力. 首先介绍AsyncTask中定义的以下几个方法: onPre ...
- Eclipse 常用快捷键 For MAC
Eclipse 常用快捷键 For MAC Option + Command + X: 运行Command + O:显示大纲Command + 1:快速修复Command + D:删除当前行Comma ...
- Python之Mac上搭建集成开发环境
首先下载一个东西: 找到下载地址:https://download.jetbrains.8686c.com/python/pycharm-professional-2016.2.1.dmg pycha ...
- 偏移:translate ,旋转:rotate,缩放 scale,不知道什么东东:lineCap 实例
<!DOCTYPE HTML> <head> <meta charset = "utf-8"> <title>canvas</ ...