当前位置:网站首页>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
边栏推荐
- Take you to brush (niuke.com) C language hundred questions (the first day)
- 学术报告系列(六) - Autonomous Driving on the journey to full autonomy
- FPGA课程:JESD204B的应用场景(干货分享)
- Abnova循环肿瘤DNA丨全血分离,基因组DNA萃取分析
- unity3d学习笔记
- 算法---比特位计数(Kotlin)
- DB2获取表信息异常:Caused by: com.ibm.db2.jcc.am.SqlException: [jcc][t4][1065][12306][4.25.13]
- Redhat5 installing vmware tools under virtual machine
- 常用函数detect_image/predict
- 场馆怎么做体育培训?
猜你喜欢
ip地址那点事
Pinduoduo lost the lawsuit: "bargain for free" infringed the right to know but did not constitute fraud, and was sentenced to pay 400 yuan
Bus消息总线
Answer to the first stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
Matlab tips (30) nonlinear fitting lsqcurefit
The latest trends of data asset management and data security at home and abroad
2022 Android interview essential knowledge points, a comprehensive summary
Stack and queue-p79-10 [2014 unified examination real question]
健身房如何提高竞争力?
MySQL的主从复制原理
随机推荐
二十岁的我4面拿到字节跳动offer,至今不敢相信
途家、木鸟、美团……民宿暑期战事将起
from . onnxruntime_ pybind11_ State Import * noqa ddddocr operation error
请教一个问题,flink oracle cdc,读取一个没有更新操作的表,隔十几秒就重复读取全量数据
使用TCP/IP四层模型进行网络传输的基本流程
2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第二阶段答案
Under what circumstances should we consider sub database and sub table
带你刷(牛客网)C语言百题(第一天)
隐马尔科夫模型(HMM)学习笔记
Get the city according to IP
[solution] final app status- undefined, exitcode- 16
C language interview to write a function to find the first public string in two strings
Matlab tips (30) nonlinear fitting lsqcurefit
Abnova循环肿瘤DNA丨全血分离,基因组DNA萃取分析
Cloudcompare point pair selection
Bus消息总线
Install mongodb database
libcurl返回curlcode说明
JWT certification
怎样查找某个外文期刊的文献?