LeetCode Counting Bits
原题链接在这里:https://leetcode.com/problems/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:
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.
题解:
Take an example: num, binary representation is 1101.
it contains two parts. The last digit, num & 1.
The other digits, 110, which has been calculated before. res[num >> 1].
Time Complexity: O(num).
Space: O(n), res array.
AC Java:
class Solution {
public int[] countBits(int num) {
int [] res = new int[num + 1];
for(int i = 1; i <= num; i++){
res[i] = (i & 1) + res[i >> 1];
}
return res;
}
}
LeetCode Counting Bits的更多相关文章
- [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
Question Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calcu ...
- 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 ...
- 2016.5.16——leetcode:Reverse Bits(超详细讲解)
leetcode:Reverse Bits 本题目收获 移位(<< >>), 或(|),与(&)计算的妙用 题目: Reverse bits of a given 3 ...
- 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 总结
最近准备刷 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 第三种 ...
随机推荐
- C语言笔记一
学习C语言已经有一段时间,然而发现越学不知道的东西越多,这是在印象笔记中记得一些东西,现在再回顾一遍顺便补充一些新东西. 一,基础知识 运算符号 优先级 单目>算术>关系 从高到低 ...
- hive数据操作
mdl是数据操作类的语言,包括向数据表加载文件,写查询结果等操作 hive有四种导入数据的方式 >从本地加载数据 LOAD DATA LOCAL INPATH './examples/files ...
- Spring事务管理
Spring是SSH中的管理员,负责管理其它框架,协调各个部分的工作.今天一起学习一下Spring的事务管理.Spring的事务管理分为声明式跟编程式.声明式就是在Spring的配置文件中进行相关配置 ...
- Pyqt5 获取命令行参数sys.argv
大家有没有注意到,很多软件都能接收第三方应用触发命令行参数,根据参数打开想要的效果. 在windows任务管理器调取命令行列,我们同样能看到进程中有好多是带有参数的. 现在,我们用Pyqt5 (Py3 ...
- win10取消vpn使用远程默认网关
打开%AppData%\Microsoft\Network\Connections\Pbk,找到rasphone.pbk文件(当你新建拨号连接或者VPN连接之后会在这个目录创建一个.pbk的文件,这个 ...
- 基于类的命令行notebook的实现
在看一本书<PYTHON3 面向对象编程> 内容丰富,作作记录. notebook.py __author__ = 'chengang882' import datetime # Stor ...
- 注解:【有连接表的】Hibernate双向1->N关联 (N端、1端同时参与控制关联关系)
Person与Address关联:双向1->N,[有连接表的],N端.1端同时参与控制关联关系 Person.java package org.crazyit.app.domain; impor ...
- 转换一个矩阵(2维数组)为HTML Table
matrix[row][col],比如[ [ "Name", "Age" ], [ "Sam", 12 ] ] function (m) { ...
- 廖雪峰js教程笔记13 插入DOM
当我们获得了某个DOM节点,想在这个DOM节点内插入新的DOM,应该如何做? 如果这个DOM节点是空的,例如,<div></div>,那么,直接使用innerHTML = '& ...
- Ado.Net事物处理
protected void Button2_Click(object sender, EventArgs e) { SqlConnection con = new SqlCo ...