LeetCode-338. Counting Bits

SSRF漏洞深度解析:原理、利用与防御实战指南 服务器端请求伪造(SSRF)是一种常见且危害性高的Web安全漏洞,其核心原理在于攻击者能够操控服务器应用程序向任意内部或外部资源发起请求。由于请求源自受信任的服务器端,SSRF攻击可以绕过客户端防火墙限制,直接访问内部网络、本地服务或云平台元数据等敏感资源。从技术价值看,理解SSRF的成因与利用方式,对于构建纵深防御体系至关重要。在应用场景上,SSRF常出现在远程资源获取、文件处理、Webhook验证及服务器端代理等功能中。本文通过剖析**云元数据服务**访问和**内部端口扫描**等典型攻击向量,深入探讨了 阅读详情

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

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

Follow Up

1.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?
2.Space complexity should be O(n).
3.Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.

Solution 1(C++)

class Solution{
public:
    vector<int> countBits(int num){
        vector<int> res(num+1, 0);
        for(int i=0; i<=num; i++){
            bitset<32> b(i);
            res[i] = b.count();
        }
        return res;
    }
};

Solution 2(C++)

class Solution{
public:
    inline bool ispowerof2(int n){
        return (n & (n-1)) ==0;
    }

    vector<int> countBits(int num){
        vector<int> dp(num+1);
        dp[0] = 0;

        if(num >= 1) dp[1] = 1;

        int curr = 2;
        int neatest = 2;
        while(curr <= num){
            nearest = ispowerof2(curr) > curr : nearest;
            dp[curr] = 1 + dp[curr-nearest];
            curr++;
        }

        return dp;
    }
};

Solution 3(C++)

class Solution{
public:
    vector<int> res(num+1, 0);
    for(int i=1; i<num+1; i++){
        res[i] = res[i/2] + i%2;
    }
    return res;
};

Solution 4(C++)

class Solution{
public:
    vector<int> countBits(int num){
        vector<int> res(num+1, 0);
        for(int i=1; i<num+1; i++){
            res[i] = res[i&(i-1)] + 1;
        }
        return res;
    }
};

算法分析

解法一

解法一使用了bitset这个数据容器,具体可以参考:

并不是十分取巧的方法,但是比较容易想出来。

解法二

解法二、解法三、解法四都是基于一点:观察如下数字:

    ^^^^
0 :  0000 -->0
1 :  0001 -->1
--------------------------
2 :  0010 -->1
3 :  0011 -->2
--------------------------
4 :  0100 -->1
5 :  0101 -->2
6 :  0110 -->2
7 :  0111 -->3
--------------------------
8 :  1000 -->1
9 :  1001 -->2
10:  1010 -->2
11:  1011 -->3
12:  1100 -->2
13:  1101 -->3
14:  1110 -->3
15:  1111 -->4
--------------------------
    ^^^^

罗列了这么多数字,可以根据横线:”———”的划分,发现0、·与1、2最低为对应相同,但是高一位前二者为0,后二者为1。同样·0、1、2、3与4、5、6、7只有最高位对应不同,前四者均为0,后四者均为1。后面的就不一一赘述了。所以假如说我要求某一个数的二进制的1的个数,可以转换为对应的一个数的二进制的1的个数加1。这个对应的数该怎么求?就是解法二、三、四要解决的问题。

解法二的做法是,用curr表示要求的数,而nearest表示离他最近的转换的分界线,也就是我在表格中横线:”———”的划分下一个数:是2的幂次数。比如,要求10,那么根据表格,可以很轻松的找到,10就是2的二进制的1个数,再加上1。而10-8=2。所以解法二中有一句:

nearest = ispowerof2(curr) ? curr  : nearest; 

就是如果curr是新的2的幂次数,那么就要更新nearest。那么状态转移方程也可求得:dp[curr] = 1 + dp[curr-nearest];

解法三

根据进一步的观察,可以换个角度来看,如果一个dp[i] = n,那么dp[i*2] = n,肯定成立,相当于将n左移一位。那么不难的到新的状态转移方程:dp[i] = dp[i/2] + i%2。

解法四

根据上面的讨论,由于0~n中的数字可以按照最高位数字不同进行转换。所以,只要把二进制最高位数字去掉,那么就可以直接找到对应的数字。所以可得到状态转移方程:dp[i] = dp[i&(i-1)] + 1。类似的题目还可以参考:

程序分析

略。

LIN协议栈 AUTOSAR架构下 状态管理 LIN State Manager 通过对这些状态的管理和转换,实现了对 LIN 总线通信状态的精确控制,以满足汽车电子系统中不同场景的需求。 阅读详情

相关推荐

实测有效!易灵思FPGA工程如何正确配置Efinity+Modelsim仿真环境

本文详细介绍了易灵思FPGA工程中如何高效配置Efinity与Modelsim联合仿真环境,包括软件版本匹配、系统环境变量设置、工程文件结构优化及仿真脚本深度优化等实战经验。通过实测数据验证,该方案可显著提升仿真效率,适用于不同规模的设计验证,帮助工程师快速搭建稳定高效的联仿环境。

m2n3b4v5c6的博客 417

杭电ACM基础题(2352、2549、2564、2565、2567、2572、2609、2607、2707、2708、2719、2721、2723)

文章目录2352、Verdis Quo[将罗马数字转化为十进制数字]2549、让你算出小数点后第n位是什么2564、词组缩写2565、放大的X[画图] 2352、Verdis Quo[将罗马数字转化为十进制数字] The Romans used letters from their Latin alphabet to represent each of the seven numerals in ...

柠檬超级酸 3437

两张 300I Duo 部署 Qwen3.5‑32B

开发者成功使用两张华为300IDuo(共4颗Ascend310P3芯片)部署Qwen3/Qwen3.5的30B/32B模型,并公开详细步骤。关键点包括:硬件需两张300IDuo(每芯片44GB显存),软件需CANN≥24.1和MindIE2.3.0专用镜像。部署流程涵盖驱动安装、模型下载(需修改为FP16格式)、容器启动、张量并行配置(TP=4)等环节。实测显示每芯片显存占用20-35GB,系统稳定运行。该方案已在知乎等平台验证可行,适用于Qwen3.5-32B等同类模型。

evering的博客 2282

#Leetcode# 338. Counting Bits

https://leetcode.com/problems/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 re...

weixin_30709809的博客 121

影刀RPA之成绩批量查询系统

自动化登录账号,读取数据表中内容去填写信息去查询,并判断多个数据不同的页面去不同的读取查询结果内容,如考生各项信息与各科成绩,总分和查询时间,采集出来到指定的数据表格中,等待时间并依次循环运行。主要功能。

2201_75330891的博客 1091

【实战揭秘】高胜率集合竞价抓涨停策略:融合MACD背离与斐波那契的量化模型

本文揭秘了一种高胜率集合竞价抓涨停策略,融合MACD背离与斐波那契数列的量化模型。通过量能异动、技术形态突破和资金共识形成的交叉验证,该策略在实盘中表现出色。文章详细解析了MACD背离的量化实现细节、斐波那契数列的实战应用,以及策略的量化建模过程,帮助投资者提升交易胜率。

herb5的博客 1608

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

李歇特冯-兹拜因巴哈的博客 140

Transformer论文解读

Transformer有多牛逼不用多说,时隔7年我们再来看看论文原文,看看这篇深刻改变世界的文章都说了什么。当然逐字逐句解读已经意义不大,我们只列考点。

xian0710830114的专栏 4412

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.

SYSU_LBY的博客 417

uniapp - 强力解决 swiper 轮播图内嵌套视频导致滑动不流畅卡顿问题,轮播滑块视图容器里既有图片也有视频video组件共同存在出现滑动切换特别卡,轮播组件包裹视频播放器加载慢及层级过高解决

uni-app,h5移动端网页,微信小程序,支付宝小程序,Android安卓,苹果ios,app,nvue,swiper,轮播图,轮播图嵌套视频,卡顿,加载慢,左右滑动不流畅,很卡,滑动过快就会卡住,uniapp使用swiper组件里嵌套视频与图片如何解决卡顿问题,详细解决教程,swiper里有视频video,播放器,视频+图片轮播,层级过高及卡顿问题,没等看完视频就自动切换了,报错,无法加载播放,uniapp的App安卓苹果端,video老是加载,APP端video组件播放视频卡顿,抖音,快手,上下滑动视

🏆 前端领域优质创作者 1万+

SAP和Oracle EBS作为全球顶级的ERP系统,在中国市场深耕数十年,尤其是在大型国企、央企和跨国企业中,已经形成了强大的用户习惯和“舒适区”依赖

国产ERP替代国际系统的关键挑战在于打破用户对SAP/Oracle的操作习惯和心理依赖。需采取多维策略:战略上明确价值主张、分阶段实施;产品上优化用户体验、确保数据迁移;实施上加强情景化培训、流程再造。成功替代70%取决于变革管理,需将"被动替换"转为"主动升级",让用户在新系统中建立更优的舒适区。

金牌架构师 387

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 760

Python爬虫技术:使用Playwright和Twitter API v2高效获取Twitter/X数据完全教程

遵守规则:始终遵循Twitter的使用条款和速率限制错误处理:实现完善的错误处理和重试机制数据质量:进行数据清洗和验证性能优化:使用异步处理和合理的延迟设置可维护性:模块化代码,便于维护和扩展。

2201_76125261的博客 674

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

蓝牙模块HM-10手册

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

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

LeetCode 338. Counting Bits 题解

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

cannonjinx的博客 1760

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

[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

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

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

Leetcode338. Counting Bits

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

magic_jiayu的博客 219

[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

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-215. Kth Largest Element in an Array
下一篇: LeetCode-494. Target Sum
zy2317878
博客等级 码龄11年 228粉丝 363原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值