LeetCode 338.Counting Bits 题解(C++)

别再只当代码阅读器了!手把手教你用Understand的实体定位器和信息浏览器,5分钟精准定位代码逻辑 本文深入解析Understand代码分析工具的高级功能,重点介绍实体定位器和信息浏览器的实战应用。通过5分钟快速定位代码逻辑的技巧,帮助开发者从代码阅读者升级为逻辑侦探,提升大型代码库的分析效率。文章包含过滤搜索、调用链追踪等实用方法,是掌握Understand核心功能的必备指南。 阅读详情

LeetCode 338.Counting Bits 题解(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.

示例

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

思路

利用十进制转换二进制的算法,在转换过程中利用temp保存取余后得到1的个数,即为该数字二进制1的个数。特别需要注意的是,在while循环结束后,需要再进行一次取余的判断,否则会丢失一个二进制1.

代码

class Solution 
{
public:
    vector<int> countBits(int num)
    {
        vector<int> arr;
        for (int i = 0; i <= num; ++i)
        {
            int temp = 0;
            int j = i;
            while (j / 2 != 0) 
            {
                if (j % 2 == 1) 
                {
                    ++temp;
                }
                j = j / 2;
            }
            if (j % 2 == 1)
            {
                ++temp;
            }
            arr.push_back(temp);
        }
        return arr;
    }
};

补充

从别人学习到的DP算法:

class Solution {  
public:  
    vector<int> countBits(int num) {  
        vector<int> result(num + 1 , 0) ;    
        for (int i = 1 ; i <= num ; i ++)   
            result[i] = result[i>>1] + i&1;    
        return result;    
    }  
};  
基于FPGA的数字图像处理-直方图操作【3.3】 对于固定的位宽和图像分辨率来说,这个计算过程是 可定量的,也就是这个归一化系数是已知的。1.直方图累计和 根据数学定义,直方图累加和的定义是小于指定像素的所有像素 的统计值之和。在实际应用中,处理帧缓存是费时费力费资源的一件事情,在许 多情况下,图像的变换比较慢,在这种情况下的一个近似是当建立当 前帧的直方图统计结果时使用从前一帧得到的映射。由测试结果可见,这两幅过曝光和曝光不足的图像,经过均衡化 之后的效果图差别不大,对比度都非常高,设计代码达到了预期的均 衡目的。累加和的计算情况如图6-28所示。 阅读详情

相关推荐

保姆级教程:用掘金量化API手把手复现一个年化17%的网格交易策略(附完整Python代码)

本文提供了一份详细的掘金量化API教程,手把手教你构建一个年化17%的网格交易策略。通过完整的Python代码实现,深入解析网格交易的核心逻辑、参数配置和动态调仓机制,帮助量化新手快速掌握策略开发全流程。教程包含环境配置、代码实现、回测分析和常见问题解答,是学习量化投资的实用指南。

weixin_33709364的博客 304

LeetCode#338 Counting Bits题解(C++版)

题干 原题网址: https://leetcode.com/problems/counting-bits/description/ 题干解析给你一个非负整数num,要你返回一个向量,这个向量的数据代表在[0, num]中每个数的二进制写法中“1”的个数。知识点逻辑难度中等解题思路这道题用暴力解法可以说是非常简单的了,但是题干要求,不能用暴力解法,要让时间复杂度和空间复杂度都是O(n),这就

游笑天涯 369

STM32 ES8266上阿里云IOT MQTT实践【第七章】:ESP8266+MQTT上阿里云物联网平台实践

本篇文章讲述了如何使用stm32接入阿里云IOT,WiFi模块为ESP8266,可以实现发布以及订阅。

超级大洋葱的博客 2万+

LeetCode //C - 338. Counting Bits

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

Made in Code 1280

合泰单片机HT32之keil5开发环境搭建(一)

1. 前言 这两天在准备合泰杯,但是总是感觉无从下手,因为合泰单片机的资料教程什么的非常少,不像stm32那样。而且听说要用合泰单片机的专用IDE,感觉当场被劝退了。后来经过查找资料,发现合泰单片机也有keil5的支持包,也就是说可以从库函数进行开发,不用从寄存器进行开发,但我找了好久,并没有找到教程,后来实在没办法,打算自己动手搭建环境,经过了大概两天的挣扎后,终于成功地搭建好了环境,也成功地点...

weixin_44713763的博客 8680

锂电池介绍

​讲述锂电池为什么会产生电以及在低温高温过充放电的问题等

m0_61973119的博客 7413

LeetCode中338Counting Bits的题解

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

codekiller_的博客 321

C#上位机实战:Modbus RTU串口通信(CRC校验+超时重发完整实现)

在工业自动化上位机开发中,Modbus RTU是串口通信的事实标准协议,SerialPort是 .NET 平台操作串口的原生类,无需依赖第三方库即可实现基础通信。实际项目中,工控现场存在电磁干扰、串口丢包、设备响应延迟等问题,CRC 数据校验用于保证报文完整性,超时重发机制用于提升通信稳定性。本文从零封装一套可直接落地的 Modbus RTU 通信组件,包含完整的校验算法、超时控制、重发逻辑,并配套 WinForm 演示界面。本文基于 C# 原生SerialPort类,实现了一套生产可用。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 402

LeetCode—338.比特位计数(Counting Bits)[中等]——分析及代码(C++/Java)

二、分析及代码 1. 动态规划 (1)思路 按照题目要求,需要计算的是 [0, num] 范围内每个二进制数字中 1 的个数,因此只需想办法建立每个数和比它小的数答案之间的联系,即可结合动态规划方法求解。 结合位运算方法,一种简单的联系是:通过 >> 1 将当前数字除以 2。 此时,第 i 个二进制数中 1 的个数 = 第 i >> 1 个二进制数中 1 的个数 + 末位 1 的个数。 (3)结果 执行用时 :64 ms, 在所有 C++ 提交中击败了 97.08% 的用户; 内存消耗 :9.4 MB, 在

江南土豆的博客 321

【vsomeip3 教程】基于 vSomeIP 3.5 的路由域实践:从配置到运行状态的完整指南

___ > 前言(读者定位与准备) 目标读者:在 Linux 上做分布式/车载中间件通信的 C++ 工程师。 > 先决条件:CMake、Boost 基础;理解 TCP/UDP 与多进程通信。 > 文中术语:Routing Host(路由进程)、Application、Service/Instance、Method、Event/EventGroup、SOME/IP-SD。 > 演示环境:单机多进程 & 跨主机两套对照。

探索C++编程的奥秘,分享深入的技术见解和实践,旨在激发读者创造力与解决问题的思维。 1179

Centos 7 Linux 离线安装单机 Kafka:下载、安装、配置、使用

本文详细介绍了在 CentOS 7 系统上离线安装单机版 Apache Kafka 的完整流程,包括环境准备(JDK 安装)、Kafka 安装包上传与解压、关键配置修改(如监听地址)、ZooKeeper 与 Kafka 服务的启动,以及 Topic 的创建、消息的生产和消费等基础操作。适用于无网络环境下的 Kafka 快速部署与学习,为后续开发或测试提供本地消息队列支持。

寒水馨 · 轻量级技术文档 · hanshuixin.org 1326

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 210

成功解决 OllamaError: status code 400, message: model ‘llama3.2:latest‘ not found 报错

本文针对Ollama更新后出现的OllamaError: status code 400, message: model 'llama3.2:latest' not found报错,分析了其根本原因——2026年1月Ollama将Llama 3.2模型命名从llama3.2:latest改为meta-llama/Llama-3.2:7b-instruct等新格式。解决方案包括:1)使用新模型名称;2)升级Ollama SDK;3)查询确认本地可用模型;4)批量更新旧项目脚本。通过正确适配新命名规则,

博客 1405

【LeetCode-338】Counting Bits(C++)

题目要求:给出一个非负整数num,求出0-num范围内的(num+1)个整数,每个整数的二进制表达中1的位数。Follow Up:时间复杂度为O(n*sizeof(integer))的解法很简单,要求实现时间复杂度为O(n)的算法。 解题方法: 1. 时间复杂度为O(n*sizeof(integer))的算法。程序如下,一共有n+1个数,每个数都要一位一位的判断是否为1,所以时间复杂度为O(n

牛遛遛 552

蓝牙模块HM-10手册

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

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

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

Ivan 的专栏 2215

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

【leetcode】338. Counting Bits(C++ & Python)

338. Counting Bits题目链接338.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 a

I am what i am 620

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

LeetCode 338. Counting Bits 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 yo

xishirujin_的博客 806

LeetCode 338. Counting Bits

今天开始,刷leetcode!一方面重新熟练编程,一方面为面试做准备!同时,记录每道题,并且要解释清楚,为的是面试能解释给面试官听!题意:求出【0,1,2,3……num】他们每个数字的二进制的1的个数思路:用举例法8的二进制是1000, 15的二进制是1111即8~15的二进制是1000~1111第一位永远是1则其countbit数量取决于000~111这个过程而000~111也就是0~7,在之前...

DrawnBreak 410

LeetCode338. Counting Bits

【代码】LeetCode338. Counting Bits。

叶卡捷琳堡的博客 615

leetcode笔记:Counting Bits

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

liyuefeilong的专栏 5047

leetcode - 338. Counting Bits

leetcode-338

sinat_41679123的博客 620

LeetCode-338. Counting Bits

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

zy2317878的博客 259

【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

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

Zarlove的博客 219

Leetcode 338. Counting Bits (Python)

Given an integern, returnan arrayansof lengthn + 1such that for eachi(0 <= i <= n),ans[i]is thenumber of1'sin the binary representation ofi. Example 1: Input: n = 2 Output: [0,1,1] Explanation: 0 --> 0 1 --> 1 2 --> 10 E...

weixin_58080442的博客 327

leetcode 338 Counting Bits【leetcode新题】 数11,C++

leetcode 338 Counting Bits【leetcode新题】 数11,C++

编程小白小卿的博客 536
下一篇: LeetCode 344. Reverse String 题解(C++)
prayyojay
博客等级 码龄10年 4粉丝 53原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值