LeetCode 338.Counting Bits 解题报告

SAP与泛微OA集成(半导体行业) 本项目通过SAP PO中间件集成SAP与泛微OA在半导体行业的应用,由于行业特殊性,广泛深入应用了RSA加密等特性 阅读详情

LeetCode 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。


示例

For num = 5 you should return [0,1,1,2,1,2].


限制条件

  • 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.

解题思路

观察以下 num<8 的结果:

num1的个数
00
11
2 - 31 2(1+1, 2的个数+1的个数)
4 - 71 2(1+1, 4的个数+1的个数) 2(1+1, 4的个数+2的个数) 3(1+2, 4的个数+3的个数)

观察上表可以得出以下规律:
1.对于 2n ,二进制表示中只在第n个bit上是1。
2.对于 m=2n+k,(k<2n) , m 的二进制表示中1的个数为1(2n的1的个数)+ k 的1的个数。
3.规律2的递推前提是0和1的情况,所以从n>=2开始可以应用规律2。


代码

class Solution {
public:
    vector<int> countBits(int num) {
        vector<int> veci;
        if (num >= 0) {
            veci.push_back(0);
        }
        if (num >= 1) {
            veci.push_back(1);
        }
        for (int i = 2; i <= num;) {
            // the case of 2^n
            veci.push_back(1);
            for (int j = 1; j < i && j <= num - i; j++) {
                // the case of m = 2^n + k, k < 2^n
                veci.push_back(1 + veci[j]);
            }
            i += i;
        }
        return veci;
    }
};

可供学习的文章:
EbowTang LeetCode OJ 338. Counting Bits

零基础入门simulink仿真建模--通信领域的编码与纠错技术--基于Simulink的CRC校验算法仿真建模示例 CRC是一种广泛应用于数字网络和存储设备中的错误检测码。它的基本原理是利用除法运算来产生固定长度的校验位,这些校验位被附加到原始数据之后发送出去。接收端收到数据后,重新计算校验值并与接收到的校验值进行比较,如果两者相同,则认为数据没有错误;否则,表示数据可能已被损坏。发送端:随机比特生成模块 + CRC生成模块。信道:模拟数据传输过程中的潜在错误(可选)。接收端:CRC校验模块 + 错误指示模块。通过本指南,我们介绍了如何基于Simulink搭建一个简单的CRC校验系统模型,并进行了仿真分析。 阅读详情

相关推荐

腾讯大佬总结的170道 python面试题

简述基于 TCP 协议的套接字通信流程。19.面向对象中`__new__` 和 `__init__` 区别。86. 解释 Python 中的`//`,`%`和`**`运算符。45.isinstance 的作用以及与 type()的区别。3. 简述下 Python 中的字符串、列表、元组和字典。143. 如何高效的找到 redis 中的某个 KEY。140. 如何保证 redis 中的数据都是热点数据。147. redis 中的 watch 的命令的作用。108. 简述 进程、线程、协程的区别以及应用场景。

AudiA6LV6的博客 493

leetcode 338. 比特位计数

__builtin_popcount class Solution { public: vector<int> countBits(int num) { vector<int>ret; for(int i=0;i<=num;i++){ ret.push_back(__builtin_popcount(i...

DUT_LYH 505

【codex教程】Codex 注册和使用教程(2026最新版)

Codex 是 OpenAI 推出的 AI 编程助手,可以帮助开发者阅读代码、修改代码、运行命令、生成测试、排查 bug、做代码审查,也可以在终端、IDE、Codex App 或网页端使用。根据 OpenAI 帮助中心,截至 2026 年 6 月,Codex 已包含在 Free、Go、Plus、Pro、Business、Edu 和 Enterprise 等 ChatGPT 方案中,但不同套餐的使用额度会有所不同。适合开发者在终端中使用。如果你想在命令行里使用 Codex,可以安装 Codex CLI。

与时俱进,开放共享 2万+

Leetcode_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: For num = 5 you sh

知行合一 600

基于MicroPython的ESP32与微信小程序MQTT通信实战

本文详细介绍了基于MicroPython的ESP32与微信小程序通过MQTT协议实现远程控制的完整实战方案。项目利用巴法云作为免费MQTT服务器,提供了从设备端编程、小程序开发到通信调试的全流程指南,帮助开发者快速构建低成本、易上手的物联网应用,如智能开关、传感器监控等。

week9的博客 642

Circular RNA的产生机制、功能及RNA-seq数据鉴定方法

《推荐关注微信公众号:AIPuFuBio,和使用免费生物信息学资源和工具AIPuFu:http://www.aipufu.com》 【Circular RNA的产生机制】 Circular RNA,缩写为circRNA,中文名为环状RNA,属于非编码RNA,是近年的一个重要研究热点。 CircRNA主要是通过backsplicing的方式产生,明显不同于线性RNA(linear RNA)经典的5′...

AIPuFu的博客 3498

LeetCode中338Counting Bits的题解

题目来源是LeetCode中的338题,题干意思很简单,就是给出一个非零整数,要求计算该整数范围内的每一个数所对应的二进制位中“1”的数量,并将其存储在一个数组中,返回数组。题目在LeetCode中属于中等难度题,因为特地要求了时间复杂度和空间复杂度均为O(n),按照常规做法很容易得到所需要的输出结果,但是时空间复杂度达不到要求。这也是该题的关键所在,把这道题的解题思路整理如下。 一、原题描

codekiller_的博客 321

第4课 SiC MOSFET的动态特性——关断过程

本文分析了SiC MOSFET的关断过程,将其划分为四个阶段:栅源电容放电阶段(t6-t7)、米勒平台阶段(t7-t8)、电压过冲阶段(t8-t9)和换流结束阶段(t9-t10)。通过等效电路详细描述了各阶段的电压电流变化特点。同时探讨了MOSFET的极间电容特性,包括输入电容Ciss、输出电容Coss和反向转移电容Crss,分析了它们对器件开关速度、损耗及电压变化速率的影响。研究指出这些非线性电容会显著影响器件的动态性能。

嵌入式老牛的博客 1062

leetcode338---比特位计数

leetcode338—比特位计数原题链接 题意简述 给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。 示例 1: 输入: 2 输出: [0,1,1] 示例 2: 输入: 5 输出: [0,1,1,2,1,2] 解题分析 位运算 HmDis(int num): 计算数字num其二进制数中的 1 的数...

Again的博客 512

Windows11 - Ubuntu 双系统及 ROS、ROS2 安装

在本教程中,我们将使用 Ubuntu 20.04 版本,该版本使用了新的 Ubuntu 桌面安装程序,该程序将包含在今后发布的所有 Ubuntu 版本中。如果你选择了这个选项,你会看到一个简单的界面,允许你选择要安装Ubuntu的硬盘,并通过滑块来决定你希望Ubuntu使用的磁盘空间大小。如果你的电脑有多个硬盘驱动器,那么只要它们各自有自己的驱动器,这个选项就能让你在安装 Ubuntu 的同时安装现有的操作系统。在此屏幕上,系统会提示您输入您的姓名和您的计算机名称,以显示在网络上。

机器人控制与仿真应用 4515

安全监控:人员行为分析_(2).人员行为分析基础理论

人员行为分析是指利用计算机视觉技术对视频中的人类行为进行实时或离线检测、识别和理解的过程。视频采集:通过摄像头等设备获取视频数据。视频预处理:对视频数据进行噪声过滤、图像增强等处理,以便后续分析。特征提取:从视频中提取出与人员行为相关的特征,如人体姿势、动作轨迹等。行为识别:利用机器学习或深度学习模型对提取的特征进行分类,识别出具体的人员行为。行为理解:对识别出的行为进行进一步分析,理解其背后的意图或状态。在人员行为分析中,常用的公开数据集包括UCF101、HMDB51、ActivityNet等。

zhubeibei168的博客 1352

(leetcode338)比特位计数(模拟法、动态规划)

原题如下: 笔者思路比较正常,是模拟正常加1情况下的数字变化,规律如下: 1、当上一个字符中有0时,下一次运算会将最右位置的0置1,将这个0的位置x后的1全部改为0; 这个字符中1的个数为为n+1-(len(s)-1-x),其中n表示上个字符中1的个数,+1表示将最右边的0置1,(len(s)-1)-x代表原来0后边的1的个数,置为0以后减去即可。 2、当上一个字符没有0时,所有1变0,同时开头补1。 此时n的个数为1。 class Solution: def countBits(self, num

lier的博客 329

3D深度饼图的QT实现

首先,我们需要创建一个QT项目并创建一个窗口,用于显示我们的饼图。在QT Creator中,选择创建一个新的QT Widgets应用程序项目,并创建一个新的窗口类。在本文中,我将介绍如何使用QT实现一个3D深度饼图(Pie 3D)的可视化效果。在构造函数中,我们将创建一个饼图(QChartView)并设置饼图的数据。这是一个简单的示例,您可以根据需要自定义和扩展代码来创建更复杂的饼图。现在,我们已经完成了3D深度饼图的QT实现。您将看到一个具有相应数据的3D深度饼图显示在窗口中。3D深度饼图的QT实现。

CodeJolt的博客 284

[leetcode] 338. 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

小榕流光的专栏 1352

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

leetcode解题报告: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: For num =

wengyunpeng的博客 623

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

LeetCode解题报告 338. Counting Bits [medium]

题目描述 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: F

conniemessi的博客 571

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

[Leetcode] 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: Fo

魔豆(Magicbean)的博客 307

LeetCode 338. Counting Bits解题报告(python)

338. Counting Bits Counting Bits python solution 题目描述 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...

Orientliu96的博客 295

【LeetCode】338. Counting Bits 解题报告(Python & Java & C++)

【LeetCode】Counting Bits 解题报告标签(空格分隔): LeetCode题目地址:https://leetcode.com/problems/counting-bits/#/description题目描述:Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ca

负雪明烛 2213

LeetCode 338. Counting Bits 题解

本题是位运算和动态规划的结合应用。利用位运算的性质动态规划状态转移时间复杂度 O(n)通过本题可以深入理解位运算的应用和动态规划的优化。

cannonjinx的博客 1760

leetcode 338. Counting Bits

来源:leetcode 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

0_o_c 761

leetcode 338 Counting Bits 详细解答

leetcode 338 Counting Bits 详细解答 以例子说明: Index :      0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 bin nums : 0 1 1 2 1 2 2 3 1 2 2  3    2   3   3   4   1

qq_22210659的博客 235

LeetCode-338. Counting Bits

LeetCode-338. Counting Bits 题目描述:https://leetcode.com/problems/counting-bits/ 解题思路 当然,如果逐位取模再判断是不是1来计算1的个数当然可以,但这样时间复杂度就是O(n*sizeof(integer)) 了,我们想要O(n) 就好。 所以,在于对给定的数num ,我们对区间0 ≤ i ≤ num中的每个数求1 的...

ke1950523491的博客 275

LeetCode //C - 338. Counting Bits

【代码】LeetCode //C - 338. Counting Bits。

Made in Code 1280

LeetCode 338. Counting Bits

338. Counting BitsGiven 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:

mingjc的博客 254

LeetCode OJ-338.Counting Bits

LeetCode OJ-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 a

oYinGui1 593
上一篇: C++ 理解复杂的声明
下一篇: LeetCode 191.Number of 1 Bits 解题报告
骆小坑
博客等级 码龄14年 55粉丝 94原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值