当前位置:网站首页>【LeetCode】338. 比特位计数
【LeetCode】338. 比特位计数
2022-06-10 01:07:00 【LawsonAbs】
0.总结
- 难点是如何实现时间复杂度为
O(n)的算法
1.题目
2.思想
2.1 暴力求解
- 遍历待求数组,得到每个数字
num - 计算每个数num 中1的含量
2.2
?
3.代码
class Solution:
def countBits(self, n: int) -> List[int]:
res = []
# standard = 1
# while(standard <= n):
# standard = standard << 1 # 左移一位
# standard = standard-1 #
# print(standard)
# 统计一的个数
def _get_cnt(num):
cnt = 0
while(num):
a,b = divmod(num,2)
num = a
if b:
cnt += 1
return cnt
for i in range(n+1):
res.append(_get_cnt(i))
return res
边栏推荐
- Mysql——》varchar
- 分布式数据库下子查询和 Join 等复杂 SQL 如何实现?
- nn. Modulelist() and nn Sequential()
- Maui + MVVM + Siemens cross platform application practice
- Beyond Compare 3密钥序列号分享及密钥被撤销的解决办法
- RHCSA第七天
- The project was successful, and the project manager was the greatest contributor?
- 我的创作纪念日
- Mysql——》事务
- Domain Adaptation and Graph Neural Networks
猜你喜欢

Win11怎么直接退回进入桌面?

Use the fiddler breakpoint to modify the request parameters and return parameters of an interface, intercept requests, and modify requests and responses

PCI BAR寄存器详解(一)

缓解修复Android Studio卡顿,Kotlin代码提示慢

孙宇晨等收购Poloniex,公链交易所双轮驱动波场生态

Win11退回Win10没有返回选项怎么办?

我的创作纪念日

Mysql——》varchar

Maui + MVVM + Siemens cross platform application practice

The state of gurobi solution and its attribute acquisition
随机推荐
星环科技科创板IPO过会:毛利率维持较高水平,腾讯等为主要股东
【ICLR 2022】Towards Continual Knowledge Learning of Language Models
进阶自动化测试的看过来,一文7个阶段5000字带你全面了解自动化测试
正则表达式不含某字符串
别再重复造轮子了,推荐使用 Google Guava 开源工具类库,真心强大!
Mongodb open source "queryable encryption" system
MySQL - transaction attributes
Luogu p2657 [scoi2009]windy number problem solving digit DP
洛谷P1028 数的计算 题解 动态规划入门题
0 is it feasible to conduct software testing only by self-study? After reading this article, I am not confused
Design and application of fire emergency lighting and evacuation indication system in a clean medicine
Batch downloading of pictures + mosaic of pictures: multiple pictures constitute the Dragon Boat Festival Ankang
花了两小时体验IDEA最新史诗皮肤
How the computer system modifies the picture format
Interview must brush algorithm top101 string chapter top30
Two problems of DataGrid
Node-RED系列(二六):使用dashboard节点开发常见的表格搜索布局
TensorFlow新文档发布:新增CLP、DTensor...最先进的模型已就绪
Curriculum Learning and Graph Neural Networks (or Graph Structure Learning)
xargs命令详解,xargs与管道的区别