当前位置:网站首页>Algorithm --- bit count (kotlin)
Algorithm --- bit count (kotlin)
2022-07-07 06:52:00 【Xiaomi technology Android research and development caoxinyu】
subject
Bit count
Give you an integer n , about 0 <= i <= n Each of the i , Calculate its binary representation 1 The number of , Returns a length of n + 1 Array of ans As the answer .
Example 1:
Input :n = 2
Output :[0,1,1]
explain :
0 --> 0
1 --> 1
2 --> 10
Example 2:
Input :n = 5
Output :[0,1,1,2,1,2]
explain :
0 --> 0
1 --> 1
2 --> 10
3 --> 11
4 --> 100
5 --> 101
Tips :
0 <= n <= 105
https://leetcode.cn/problems/counting-bits/
Solutions
2 Of n The next power There must be only one 1
Because it's all 10000
Whenever more than one 2 Of n The next power
We can put the excess part
Find the result from the previous array
For example, over 2, So the current result is actually 1 + int[2]
resolvent
fun countBits(n: Int): IntArray {
val intArray = IntArray(n + 1)
var highBit = 0
for (i in 1..n) {
if ((i and (i - 1)) == 0) {
intArray[i] = 1
highBit = i
} else {
intArray[i] = intArray[i - highBit] + 1
}
}
return intArray
}
summary
1. How to judge a number is 2 Of n The next power ?
x&(x-1) = 0 It means 2 Of n The next power
边栏推荐
- [noi simulation] regional division (conclusion, structure)
- MySQL的安装
- Comment les entreprises gèrent - elles les données? Partager les leçons tirées des quatre aspects de la gouvernance des données
- 化工园区危化品企业安全风险智能化管控平台建设四大目标
- 企业如何进行数据治理?分享数据治理4个方面的经验总结
- MYSQL binlog相关命令
- Which foreign language periodicals are famous in geology?
- Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]
- Can 7-day zero foundation prove HCIA? Huawei certification system learning path sharing
- 【luogu P1971】兔兔与蛋蛋游戏(二分图博弈)
猜你喜欢

.net 5 FluentFTP连接FTP失败问题:This operation is only allowed using a successfully authenticated context

带你刷(牛客网)C语言百题(第一天)

MySQL view bin log and recover data

Brand · consultation standardization

MOS管参数μCox得到的一种方法
![[GNN] graphic gnn:a gender Introduction (including video)](/img/b3/0855885dafa7afaa7375b8d2195974.png)
[GNN] graphic gnn:a gender Introduction (including video)

2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第二阶段答案

jdbc数据库连接池使用问题
![Stack and queue-p78-8 [2011 unified examination true question]](/img/df/72ba22f1953551943494d661a56a3b.jpg)
Stack and queue-p78-8 [2011 unified examination true question]

Redhat5 installing vmware tools under virtual machine
随机推荐
Maze games based on JS
Data of all class a scenic spots in China in 2022 (13604)
Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]
Postgresql源码(60)事务系统总结
快速定量,Abbkine 蛋白质定量试剂盒BCA法来了!
品牌·咨询标准化
C language interview to write a function to find the first occurrence of substring m in string n.
「运维有小邓」符合GDPR的合规要求
【luogu P1971】兔兔与蛋蛋游戏(二分图博弈)
地质学类比较有名的外文期刊有哪些?
MySQL user permissions
[start from scratch] detailed process of deploying yolov5 in win10 system (CPU, no GPU)
RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling `cublasCreate(handle)`问题解决
How to find the literature of a foreign language journal?
Matlab tips (29) polynomial fitting plotfit
Abnova 免疫组化服务解决方案
【从零开始】win10系统部署Yolov5详细过程(CPU,无GPU)
联合索引ABC的几种索引利用情况
mysql查看bin log 并恢复数据
linux系统rpm方式安装的mysql启动失败