当前位置:网站首页>[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
2022-07-06 12:51:00 【Deng Jiawen jarvan】
[ Algorithm ] The finger of the sword offer2 golang Interview questions 3: front n A number in binary form 1 The number of
subject 1:
Given a nonnegative integer n , Please calculate 0 To n In the binary representation of each number between 1 The number of , And output an array .
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
explain :
0 <= n <= 105
func countBits(n int) []int {
}
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/w3tCBm
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas 1:
Ideas 1: Violence counts every digit of every number 1 The number of , Time complexity O(n*17),10^5 < ^17
Code
func countBits(n int) []int {
// Ideas 1: Violence counts every digit of every number 1 The number of , Time complexity O(n*17),10^5 < ^17
// Processing parameters
if n == 0 {
return []int{
0}
}
res := make([]int,n+1)
for i:=0;i <=n;i++{
count := 0
for j:=0;j<17;j ++{
if i & (1 << j) != 0 {
count ++
}
}
res[i] = count
}
return res
}
test
Ideas 2:
// The main points of : i&(i-1) It's more than i in 1 One less number
// We can get through the previous i in 1 To find the number of , The time complexity is O(n)
Code 2
func countBits(n int) []int {
// The main points of : i&(i-1) It's more than i in 1 One less number
// We can get through the previous i in 1 To find the number of , The time complexity is O(n)
if n == 0 {
return []int{
0}
}
res := make([]int,n+1)
res[0] = 0
for i:=1;i <=n;i++{
res[i] = res[i&(i-1)] + 1
}
return res
}
test 2
边栏推荐
- Expected value (EV)
- [Leetcode15]三数之和
- Agile development helps me
- Unity3d makes the registration login interface and realizes the scene jump
- C programming exercise
- Unity3D摄像机,键盘控制前后左右上下移动,鼠标控制旋转、放缩
- The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
- [899]有序队列
- 【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
- (课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
猜你喜欢
Single chip Bluetooth wireless burning
Database course design: college educational administration management system (including code)
Prove the time complexity of heap sorting
(core focus of software engineering review) Chapter V detailed design exercises
音乐播放(Toggle && PlayerPrefs)
(1) Introduction Guide to R language - the first step of data analysis
Conditional probability
[algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
随机推荐
Game 280 weekly
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
NovAtel 板卡OEM617D配置步骤记录
Excel导入,导出功能实现
Acwing-116 pilot brother
341. Flatten nested list iterator
VLSM variable length subnet mask partition tips
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
Fairygui character status Popup
基于rtklib源码进行片上移植的思路分享
(the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
[Offer29] 排序的循环链表
[Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
HCIP Day 12
Introduction to the daily practice column of the Blue Bridge Cup
Agile development helps me
(core focus of software engineering review) Chapter V detailed design exercises
[offer78] merge multiple ordered linked lists
How to reduce the shutdown time of InnoDB database?