当前位置:网站首页>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
边栏推荐
- MYSQL binlog相关命令
- Can't you really do it when you are 35 years old?
- 大促过后,销量与流量兼具,是否真的高枕无忧?
- Cloudcompare point pair selection
- How to share the same storage among multiple kubernetes clusters
- MOS tube parameters μ A method of Cox
- mysql查看bin log 并恢复数据
- The latest trends of data asset management and data security at home and abroad
- 怎样查找某个外文期刊的文献?
- .net core 访问不常见的静态文件类型(MIME 类型)
猜你喜欢

关于数据库数据转移的问题,求各位解答下

mysql查看bin log 并恢复数据

Jetpack Compose 远不止是一个UI框架这么简单~

MySQL的主从复制原理
![Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]](/img/13/096857158c9f977f8677f7cd0f9d4b.png)
Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]

The latest trends of data asset management and data security at home and abroad

String (explanation)

Data of all class a scenic spots in China in 2022 (13604)

Prompt for channel security on the super-v / device defender side when installing vmmare

大咖云集|NextArch基金会云开发Meetup来啦
随机推荐
Install mongodb database
2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第二阶段答案
[noi simulation] regional division (conclusion, structure)
【从零开始】win10系统部署Yolov5详细过程(CPU,无GPU)
Prompt for channel security on the super-v / device defender side when installing vmmare
LVS+Keepalived(DR模式)学习笔记
请问如何查一篇外文文献的DOI号?
服装门店如何盈利?
Which foreign language periodicals are famous in geology?
impdp的transform参数的测试
JWT certification
C interview 24 (pointer) define a double array with 20 elements a
循环肿瘤细胞——Abnova 解决方案来啦
学术报告系列(六) - Autonomous Driving on the journey to full autonomy
化工园区危化品企业安全风险智能化管控平台建设四大目标
Postgresql中procedure支持事务语法(实例&分析)
Programmers' daily | daily anecdotes
Matlab tips (30) nonlinear fitting lsqcurefit
Config分布式配置中心
How to find the literature of a foreign language journal?