当前位置:网站首页>[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
边栏推荐
- [Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
- 抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
- Office提示您的许可证不是正版弹框解决
- Meanings and differences of PV, UV, IP, VV, CV
- idea问题记录
- GPS高程拟合抗差中误差的求取代码实现
- Vulnhub target: hacknos_ PLAYER V1.1
- Unity3d, Alibaba cloud server, platform configuration
- Unity3D制作注册登录界面,并实现场景跳转
- 第一人称视角的角色移动
猜你喜欢
Unity3D,阿里云服务器,平台配置
使用rtknavi进行RT-PPP测试
NovAtel 板卡OEM617D配置步骤记录
2021.11.10 compilation examination
Derivation of logistic regression theory
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
There is no red exclamation mark after SVN update
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
[算法] 剑指offer2 golang 面试题1:整数除法
随机推荐
[Offer29] 排序的循环链表
SSD technical features
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
Special palindromes of daily practice of Blue Bridge Cup
Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
GNSS定位精度指标计算
[offer9] implement queues with two stacks
FGUI工程打包发布&导入Unity&将UI显示出来的方式
Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
FairyGUI人物状态弹窗
What is the maximum length of MySQL varchar field
Fairygui joystick
Database table splitting strategy
Unity3D制作注册登录界面,并实现场景跳转
Guided package method in idea
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
Naive Bayesian theory derivation
MySQL performance tuning - dirty page refresh
基于rtklib源码进行片上移植的思路分享
Easy to use shortcut keys in idea