当前位置:网站首页>[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

边栏推荐
- Design and implementation of general interface open platform - (39) simple and crude implementation of API services
- 【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
- Get the position of the nth occurrence of the string
- [Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
- Liste des boucles de l'interface graphique de défaillance
- 服务未正常关闭导致端口被占用
- Fairygui loop list
- FairyGUI簡單背包的制作
- MySQL error warning: a long semaphore wait
- 1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
猜你喜欢
随机推荐
Get the position of the nth occurrence of the string
[Leetcode15]三数之和
[offer9]用两个栈实现队列
地球围绕太阳转
Conditional probability
[offer18] delete the node of the linked list
dosbox第一次使用
[leetcode15] sum of three numbers
[offer78]合并多个有序链表
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
[offer78] merge multiple ordered linked lists
JUC forkjoin and completable future
Compile GDAL source code with nmake (win10, vs2022)
Meanings and differences of PV, UV, IP, VV, CV
2021.11.10 compilation examination
Game 280 weekly
Expected value (EV)
Acwing-116 pilot brother
Theoretical derivation of support vector machine
程序设计大作业:教务管理系统(C语言)




![[Chongqing Guangdong education] Shandong University College Physics reference materials](/img/56/4ac44729c3e480a4f779d6a890363a.jpg)



