leetcode 338. Counting Bits

基于STM32L4XX、HAL库的 L76KB-A58定位模块驱动程序设计 / 确保字符串终止。// 缓冲区溢出,重置。// 重置缓冲区索引。支持GPS、GLONASS、Galileo、北斗和QZSS多系统定位。case 1: // UTC时间。// 配置GPS模块(可选)// 查找GGA语句。// 启动UART接收。// USART1初始化函数。内置LNA和SAW滤波器。 阅读详情

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

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

下面代码运行到8时报runtime error,目测是时间复杂度的问题,需要换一种算法,不能暴力破解,明天再继续来解,加油~~~

int* countBits(int num, int* returnSize) {
    *returnSize=num+1;
    int*arr=(int*)malloc(num+1);
    int i,j;
    for(i=0;i<num+1;i++){
		arr[i]=0;
        j=i;
        while(j!=0){
            if(j%2==1)
                arr[i]++;
            j=j/2;
        }
    }
    return arr;
}


有第二种方法,但依旧没通过,还是报runtime error的错误,下次再来看这题了~

参考:LeetCode — 338. Counting Bits

思路:举例子说明:

1、最低位为0,加1之后,1的个数直接加1;

2、(i-1)次低位和最低位为01,加1之后为i,而这两低位变为10,i&(i-1)之后直接将有进位的进行了屏蔽;

3、(i-1)最后3位为011,加1之后为i,最低三位变为100,i&(i-1)之后直接将有进位的进行了屏蔽,而arr[i&(i-1)]+1即为所求值,其中+1为进位的最高位的补偿

/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* countBits(int num, int* returnSize) {
    *returnSize=num+1;
    int*arr=(int*)malloc(num+1);
    arr[0]=0;
    int i;
    for(i=1;i<num+1;i++){
		arr[i]=arr[i&(i-1)]+1;
    }
    return arr;
}

在stackoverflow上提出了这个问题,原来是malloc的用法问题,一段时间不写c什么都忘了。malloc返回void*的指针,void*可强制转换为任意指针类型。

例如:

int*arr=(int*)malloc(sizeof(int)*(num+1));

表示为arr分配num+1字节的空间,然后强制转换为(int*)类型的指针

把上面的两种代码中分配空间代码改为上面这句就都可以通过了

【数据湖仓】:Iceberg、Hudi 和 Paimon 对比 适用于需要多计算引擎支持和复杂查询的离线批处理场景。其中立性和灵活性使其成为构建新型数据平台的理想选择。:在需要高频数据更新和高效小文件管理的场景中表现出色,适合实时数据处理和增量数据更新的业务需求。:专为实时流式数据处理设计,适用于对数据实时性要求高的业务场景,如实时数据分析和实时指标监控。通过以上分析,可以看出 Apache Iceberg、Apache Hudi 和 Apache Paimon 各有其独特的优势和适用场景。企业应根据自身业务需求和技术栈选择合适的数据湖仓方案,以充分发挥其价值。 阅读详情

相关推荐

Zotero on Mac: 高效文献管理与引用指南

本文为Mac用户提供了一份全面的Zotero高效文献管理与引用指南。从安装配置、文献收集管理、PDF深度阅读与笔记,到在Word中无缝插入引用和生成参考文献,详细讲解了核心工作流。同时推荐了ZotFile、Better BibTeX等必备插件,并介绍了利用同步与WebDAV实现跨设备无缝衔接的方法,助力研究者构建个人数字知识库,极大提升学术研究效率。

weixin_29281941的博客 1192

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

深入浅出:UE4类型系统完整构建过程详解

UE4的类型系统是一个复杂但设计精巧的架构,它通过静态代码生成和运行时构建的完美配合,实现了强大的反射功能。理解这个过程不仅有助于解决开发中的问题,也能让你更好地利用UE4的各种高级特性。就像建造一栋大楼需要经过设计、备料、框架、装修、验收等多个阶段,UE4类型系统的构建也是一个多阶段、精细化的过程。每个阶段都有其特定的职责,共同协作完成从源代码到运行时对象的转换。

m0_45371381的博客 922

LeetCode 338. Counting Bits 题解

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

cannonjinx的博客 1760

MTK眼中6G物理层与无线协议的进化之路

提议统一波束和小区级的测量、报告和切换框架/流程,进一步减少移动性切换过程中的中断,采用可扩展框架/流程,允许在不同移动性场景中进行部分用户设备配置更新以实现快速切换,支持用户设备发起的移动性(同步/切换)以降低延迟和因信令交换导致的失败,设计移动性RS以实现空闲状态下的节能和连接状态下的无缝用户体验,实现5G/6G间的无缝互操作移动性,为固定无线接入(FWA)提供潜在增强,构建统一的跨层移动性框架以降低复杂度、减少中断和失败。为适应多样化的流量类型,需要灵活的帧结构和高层时间对齐。

asd199086的博客 1758

绝缘检测原理和绝缘电阻计算方法

绝缘检测的目的就是计算绝缘电阻,当绝缘电阻低于某个阈值,则很可能会造成电池短路、漏电,威胁人员安全

m0_56208280的博客 4万+

[leetcode]338. Counting Bits

[leetcode]338. Counting Bits Analysis 周五ummmmmm—— [啊啊啊啊 paper结果要出来了,心塞] Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in ...

weixin_32135877的博客 164

Logisim实战:手把手教你设计硬布线版MIPS CPU(多周期/8指令/附电路图)

本文是一份详细的Logisim实战指南,手把手教你设计一个采用硬布线控制的多周期MIPS CPU。文章深入剖析了硬布线控制与微程序控制的核心理念差异,并逐步讲解了从定义执行状态、构建状态跳转逻辑到生成控制信号、集成测试的完整流程,最终实现一个支持8条指令、可运行简单程序的处理器模型。

g9h0i1的博客 604

LeetCode-338. Counting Bits [C++][Java]

Given an integern, returnan arrayansof lengthn + 1such that for eachi(0 <= i <= n),ans[i]is thenumber of1'sin the binary representation ofi.

贫道绝缘子的博客 966

CCS20实战入门:第一个工程搭建示例

通过实际操作演示如何在ccs20中创建并配置首个开发项目,涵盖环境设置与基础调试步骤,帮助新手快速上手ccs20集成开发环境,提升嵌入式开发效率。

weixin_31185473的博客 896

图神经网络学习笔记-点云数据处理(专题七)

本教程解释了如何利用图神经网络(Graph Neural Networks,GNNs)来处理和训练点云数据。尽管点云默认情况下不具有图结构,但我们可以利用 PyG 的转换工具,将其转化为适合 PyG 所提供的全套 GNN 模型的形式。关键思想是从点云数据中创建一个,通过 GNN 的消息传递机制,从中学习有意义的局部几何结构。这些点的表示可以被用于例如点云分类或分割等任务。

357

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

03. 线性规划与混合整数规划 (LP/MIP)

本文介绍了OR-Tools中的线性规划(LP)和混合整数规划(MIP)求解器。主要内容包括:1) LP/MIP的基本概念和数学形式,对比了LP、MIP、ILP和BIP的区别;2) OR-Tools支持的求解器类型,如GLOP、SCIP等,以及它们各自的特点和适用场景;3) 详细展示了如何使用GLOP求解器进行线性规划求解,包括变量定义、约束添加和目标函数设置;4) 介绍了SCIP求解器的配置和使用方法;5) 讨论了变量类型、约束类型、多目标优化等进阶功能。文章通过Java代码示例演示了如何实现一个完整的优化

智慧是做事用的 167

Leetcode338. Counting Bits

计算二级制数中 1 的数目。

magic_jiayu的博客 219

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

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

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

[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的专栏 609

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

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

LeetCode 338.Counting Bits 解题报告

LeetCode 338.Counting Bits 解题报告

骆小坑的填坑之旅 579

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.

prayyojay的博客 333

【LeetCode】338Counting Bits

【链接】: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.Exa

herongwei 的 BLOG 605

Leetcode 338. Counting Bits (Medium) (cpp)

Leetcode 338. Counting Bits (Medium) (cpp)

还是需要多学习!闷声大发财,这是坠吼的! 876

[LeetCode]338. Counting Bits

[LeetCode]338. Counting Bits思路对于每一位数,计算其中1的个数,计算过程优化,可以使时间较短代码class Solution { public: vector<int> countBits(int num) { vector<int> res; for (int i = 0; i < num + 1; ++i) {

Lcharon的博客 3222

LeetCode oj 338. Counting Bits(DP)

338. Counting Bits QuestionEditorial Solution My Submissions Total Accepted: 46702 Total Submissions: 79672 Difficulty: Medium Given a non negative integer number num. For every numbers i in the r

Tristeza的博客 461

LeetCode中338Counting Bits的题解

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

codekiller_的博客 321

[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

TstsUgeg的博客 3386

LeetCode Top 100 Liked Questions 338. Counting Bits (Java版; Medium)

welcome to my blog LeetCode Top 100 Liked Questions 338. Counting Bits (Java版; Medium) 题目描述 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number ...

littlehaes的博客 225
上一篇: 344. Reverse String
下一篇: Leetcode-104. Maximum Depth of Binary Tree c语言
0_o_c
博客等级 码龄12年 60粉丝 175原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值