leetCode #338 - Counting Bits

信息学奥赛刷题必备:OpenJudge NOI 4.6 1455题‘An Easy Problem’保姆级解法(附C++代码) 本文深入解析信息学奥赛经典题目OpenJudge NOI 4.6 1455题'An Easy Problem',提供两种高效解法:暴力枚举法和位运算规律法,并附详细C++代码实现。通过数制转换和枚举技巧,帮助参赛者掌握二进制数位操作的核心算法,提升竞赛解题能力。 阅读详情

题目描述:

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.

求1~n每个数的为1 的位数,时间复杂的为O(n*sizeof(integer)),空间复杂度为O(n)。

class Solution {
public:
    vector<int> countBits(int num) {
        vector<int> bits_num(num+1,0);
        for(int i=0;i<=num;i++)
            bits_num[i]=get_bits_num(i);
        return bits_num;
    }
    
    int get_bits_num(int num)
    {
        int count=0;
        while(num>0)
        {
            if(num%2==1) count++;
            num/=2;
        }
        return count;
    }
};

别再死记硬背了!一张图搞懂无刷电机半桥与全桥驱动的核心差异(附避坑指南) 本文深入解析无刷直流电机的半桥与全桥驱动方式的核心差异,提供直观对比与选型指南。半桥驱动结构简单、成本低,适合低要求场景;全桥驱动性能优越但复杂,适用于高精度控制。文章还包含实战避坑指南,帮助工程师优化驱动方案选择与应用。 阅读详情

相关推荐

单卡调试OpenGait:从分布式到本地的轻量级改造指南

本文为个人开发者和研究者提供了一份详细的OpenGait框架单卡调试指南。针对该框架默认依赖多GPU分布式训练的问题,文章通过分析核心痛点,逐步讲解了如何从环境搭建、绕过分布式初始化陷阱、处理通信后端报错到最终配置与调试,实现从分布式到本地的轻量级改造,帮助读者在单张GPU上高效进行步态识别模型的验证与学习。

weixin_28223453的博客 148

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

ccystewart的博客 393

最新Mamba环境配置教程:从CUDA到causal-conv1d的完整安装流程

本文提供最新Mamba环境配置教程,涵盖从CUDA到causal-conv1d的完整安装流程。针对深度学习领域的长序列处理需求,详细指导如何正确配置CUDA工具链、安装PyTorch及关键依赖causal-conv1d,解决常见报错问题,并验证mamba-ssm集成效果。

bush的博客 424

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

小昕成长史 202

Lm317电压源芯片制作电流源

还在找电流源芯片么?如何使用电压源芯片快速搭建电流源。

Ekko的博客 1万+

手把手教你用vLLM在昇腾910B上部署Qwen3-Embedding-8B模型(保姆级避坑指南)

本文详细介绍了如何在昇腾910B芯片上使用vLLM推理引擎部署Qwen3-Embedding-8B模型的全流程,包括环境准备、vLLM-ascend定制化安装、模型获取与校验、环境变量配置、服务启动参数优化、性能压测及生产级部署建议。特别针对昇腾生态的开发者,提供了保姆级的避坑指南和实用技巧,帮助实现高效稳定的模型部署。

weixin_28726671的博客 472

Leetcode #338 - Counting bits - Medium

ProblemGiven 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. ExampleFor num = 5 y

Arcome的博客 515

免费获取船舶轨迹数据?手把手教你用Python处理中国海洋卫星AIS数据

本文详细介绍了如何免费获取中国海洋卫星AIS数据,并使用Python进行船舶轨迹数据的处理与分析。从数据源评估到自动化处理流水线,再到高级分析与可视化技巧,手把手教你避开常见问题,实现高效数据处理。特别适合航运数据分析、物流优化和海上安全研究等领域的研究者和开发者。

weixin_42616115的博客 345

Leetcode Algorithm Problem #338. Counting Bits

Leetcode Algorithm Problem #338. Counting Bits

q_2965574080的博客 164

低供电电压CMOS偏置电路分析(有些类似于三支路电流源)

1、偏置电路原理图以及仿真结果 电路电流分析:Ip=Ip1=Ip2=Ip3,那么 Ip=12μnCoxWL(IpR−VTH)2 I_p=\frac{1}{2} μ_nC_{ox}\frac{W}{L} (I_{p}R-V_{TH})^2 Ip​=21​μn​Cox​LW​(Ip​R−VTH​)2 解出方程式即可得到参考电流及参考电压 2、电路电源抑制比PSRR分析 这里为了方便,小信号前都不加△ 首先做一个近似,由于gmRo>>1,直接将VDD变化导致的P3管漏级电压变化与VDD变化量做相等

qq_46579389的博客 2916

JMeter 中实现 100 个用户在 3 秒内并发登录

通过以上配置,你可以在 JMeter 中实现 100 个用户在 3 秒内并发登录

南开大学 | 10 年 + 软件研发与测试实战 | 现任企业质控负责人 | 专注端到端交付质量 1695

LeetCode 338. Counting Bits 题解

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

cannonjinx的博客 1760

备战2025数学建模国赛(算法65):Fisher判别分析

在数学建模竞赛中,我们常常会遇到需要对研究对象进行分类或判断的问题。例如,根据西瓜的各类指标判断其好坏,根据经济数据判断一个国家的发展程度,或根据病人的医学影像特征判断其是否患病。这类“分类判别”问题,是建模竞赛中最常见的题型之一。Fisher判别分析(或称线性判别分析,LDA)作为一种经典的、有监督的降维与分类方法,因其模型简洁、可解释性强、计算高效而备受青睐。

YOLO 426

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 an array...

Wisimer 211

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

inori mmd 7z

楪祈的模型资源....................................................................................................................................................................................................................

[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 an ar

Smaug的专栏 610

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

【python】【leetcode】【算法题目338—Counting Bits】

一、题目描述 题目原文:Given a non negative integer numbernum. For every numbersiin the range0 ≤ i ≤ numcalculate the number of 1's in their binary representation and return them as an array.(给出一个非负整数n

一只奋斗的小虫的博客 238

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

LeetCode 338. 比特位计数(Counting Bits)

LeetCode.jpg 338. 比特位计数 338. 比特位计数 给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。 示例 1: 输入: 2 输出: [0,1,1] 示例 2: 输入: 5 输出: [0,1,1,2,1,2] ...

墨1024 214

leetcode(338). Counting Bits

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

qishi的博客 458

338. counting bits java_【LeetCode】Counting Bits(338)

&lpar; C&plus;&plus;&rpar; Access the hard drive&period;// Open up the volume HANDLE hVolume = CreateFile(wstrPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHA ...MySQL存储过程调试工具-dbForg...

weixin_35041325的博客 242

leetcode笔记:Counting Bits

题目大意是,给定一个非负整数num,对于每一个满足0 ≤ i ≤ num的数字i,计算这些数字的二进制表示中1的个数,并以数组vector的形式返回。

liyuefeilong的专栏 5047

【LeetCode】338. Counting Bits

问题描述问题链接:https://leetcode.com/problems/counting-bits/#/descriptionGiven 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 repre

墨染百城 1360

LeetCode338: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 1: Input: 2 Out...

卷卷萌的博客 216

leetcode 338 Counting Bits python3 多种(最简)解法,寻求数学规律

”’ 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...

Jasmine_dream 511

【leetcode】338. Counting Bits

21.58%   时间复杂度没有按要求note:while判断的时候,首先要满足的条件放and左边class Solution(object): def countBits(self, num): """ :type num: int :rtype: List[int] """ result = [] ...

u014381464的博客 144

leetcode-338. Counting Bits 统计十进制数字的二进制表示中1的个数,列表的定义,奇偶判断

题目: 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

qqzj_bupt的博客 379

leetcode 338. Bit位计数 Counting Bits python3 最简代码(一行代码) bin()、count()

# 一次遍历 return [bin(i)[2:].count('1') for i in range(num+1)]

Jasmine_dream 748
上一篇: LeetCode #337 - House Robber III
下一篇: LeetCode #347 - Top K Frequent Elements
LawFile
博客等级 码龄10年 5粉丝 627原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址