[LeetCode] Counting Bits

news/2023/12/9 17:24:44

Problem

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.

Hint

You should make use of what you have produced already.
Divide the numbers in ranges like [2-3], [4-7], [8-15] and so on. And try to generate new range from previous.
Or does the odd/even status of the number help you in calculating the number of 1s?

Note

应用公式f[i] = f[i/2] + (i%2);
并优化此公式为f[i] = f[i>>2] + (i&1),减少计算时间。

Solution

public class Solution {
    public int[] countBits(int num) {
        int[] dp = new int[num+1];
        for (int i = 1; i <= num; i++) dp[i] = dp[i>>1] + (i&1);
        return dp;
    }
}

http://www.niftyadmin.cn/n/2525440.html

相关文章

Low-level I/O 和 File System Interface

Low-level I/O 和 File System Interface 2008-10-16 15:09 482人阅读 评论(1) 收藏 举报fileinterfacesystemdescriptorstructstream1. 为什么需要使用 low-level I/O&#xff0c;glibc 里面提到了一些情形&#xff0c;对大量二进制数据进行操作&#xff0c;某些文件上的操作只…

CG cosh, exp, sinh, smoothstep, tanh, perlin_easeCurve1/2 曲线

文章目录cosh为何叫双曲线exp(x)曲线exp(-x) 曲线exp(x)exp(-x) 两曲线叠加调整x0时&#xff0c;y0调整x-2 or 2时&#xff0c;y1exp(-x*x)sinhsmoothsteptanhperlin noise ease curve 1perlin noise ease curve 2Summaryexcel 文件Referencesxxxh 中的h>hyperbolic&#xf…

Bash的输入输出重定向

Bash的输入输出重定向 此页由Linux Wiki用户Chenxing于2012年12月18日 (星期二) 07:58的最后更改。 在linuxCook的工作基础上。使用Bash可以方便的用<和>实现输出输入的重定向&#xff0c;本文讨论重定向的一些细节和技巧。本文介绍部分是对Bash Quick Reference相关内容…

Unity Shader PostProcessing - 1 - 后处理概念

(加粗的内容需要留意) 什么叫PostProcessing&#xff1f; Post Processing 中文直译&#xff1a;后处理&#xff08;以前我是不知道是怎么回事&#xff0c;总觉得好高端的感觉&#xff0c;-_-&#xff09; 我们知道&#xff1a;在场景的所有需要渲染的对象&#xff0c;渲染完后…

文件描述符和文件流之间的转换

文件描述符和文件流之间的转换 2009-11-24 17:30 345人阅读 评论(0) 收藏 举报streamfilesocketFILE *fdopen(int fildes, const char *type); 这个函数很有用的&#xff0c;功能是将一个流关联到一个打开的文件号filedes上&#xff0c; 该filedes可以是open、pipe、dup、dup2…

Unity Shader PostProcessing - 2 - 边缘检测

边缘检测的算法有很多种 这里介绍的是其中的一部分 SobelPrewittRobertLine 这些卷积核&#xff0c;都是final g越大&#xff0c;越可能是边缘 Sobel (-1,1), (0,1), (1,1)与(-1,-1),(0,-1),(1,-1)的亮度差距越大&#xff0c;特别是左右、上下的差异越大&#xff0c;则final…

switch表达式类型

switch表达式类型 一般格式&#xff1a; switch &#xff08;表达式&#xff09; &#xff5b; case 常量标号1&#xff1a;语句序列1; break; case 常量标号2&#xff1a;语句序列2; break; … case 常量标号n&#xff1a;语句序列n; break; default&#xff1a; 语句S; &…

iOS开发系列之运动事件

前面我们主要介绍了触摸事件以及由触摸事件引出的手势识别&#xff0c;下面我们简单介绍一下运动事件。在iOS中和运动相关的有三个事件:开始运动、结束运动、取消运动。 监听运动事件对于UI控件有个前提就是监听对象必须是第一响应者&#xff08;对于UIViewController视图控制器…

C语言 side effect 和 sequence point

C语言 side effect 和 sequence point 2011-09-18 16:16 126人阅读 评论(0) 收藏 举报语言cobject编译器functionfileC 语言中&#xff0c;术语副作用&#xff08;side effect&#xff09;是指对数据对象或者文件的修改。例如&#xff0c;以下语句 var 99;的副作用是把…

L3-005. 垃圾箱分布

L3-005. 垃圾箱分布 题目链接&#xff1a;https://www.patest.cn/contests/gplt/L3-005 Dijstra 与L2-001.紧急救援类似&#xff0c;是Dijstra最短路的拓展&#xff0c;要注意排序是按照&#xff1a; 1.到所有居民点的最小距离的最大&#xff1b; 2.平均距离最短&#xff1b; 3…

java-对象的理解-对象在内存的存储

String str; 这段代码表示创建一个名叫str的String类型的reference。 这个reference保存在栈stack中&#xff0c;速度很快&#xff0c;但它并没有保存词或者句子的数据&#xff0c;它仅仅是目标数据的一个指向。 如果此时向str发送一个消息&#xff0c;就会返回一个运行时错误&…

怎么设置自定义Tabbar的文字大小 -------ios

系统默认的tabbar感觉好小&#xff0c;很丑&#xff0c;于是得自己来设置它的大小 我们需要在 (void)load 或者在 (void)initialize 里设置 那这两个方法有什么区别呢 可以看这里 &#xff1a;&#xff08;转载&#xff09;http://www.jianshu.com/p/9368ce9bb8f9 我们在这里…

删除两个双向循环链表的相同节点

删除两个双向循环链表的相同节点 分类&#xff1a; Data Structure 面试题集2011-09-23 12:37 1242人阅读 评论(1) 收藏 举报nulldeletestructsystemc有两个双向循环链表A&#xff0c;B&#xff0c;知道其头指针为&#xff1a;pHeadA&#xff0c;pHeadB&#xff0c;请写一个函数…

Unity Shader - Simple Toon Shading - 简单卡通渲染

文章目录最终效果 - Final Effect无光照&#xff0c;只有纹理与主色调Shader加描边 - OutlineGIFShader添加光影 - RecieveShadow自身接收阴影Shader调整阴影 - Adjusting Shadow ParamsShader无透视法线挤出描边Shader整体运行效果高光 - SpecularShader边缘光 - RimShader控制…

javascript(十五) 错误处理技术

为什么80%的码农都做不了架构师&#xff1f;>>> 错误处理技术 常见的异常EvalError&#xff0c; RangeError &#xff0c;ReferenceError &#xff0c;SyntaxError &#xff0c;TypeError &#xff0c;URIError 其他就是语法规则try &#xff5b;&#xff5d;catch…

给定一个存放整数的数组,重新排列数组使得数组左边为奇数,右边为偶数

给定一个存放整数的数组&#xff0c;重新排列数组使得数组左边为奇数&#xff0c;右边为偶数 分类&#xff1a; c/c 数据结构算法2011-07-16 22:40 594人阅读 评论(0) 收藏 举报[cpp] view plaincopyprint? /* 给定一个存放整数的数组&#xff0c;重新排列数组使得数组左边为…
最新文章