当前位置:网站首页>[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
2022-07-06 09:18:00 【邓嘉文Jarvan】
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
题目1:
给定一个非负整数 n ,请计算 0 到 n 之间的每个数字的二进制表示中 1 的个数,并输出一个数组。
示例 1:
输入: n = 2
输出: [0,1,1]
解释:
0 --> 0
1 --> 1
2 --> 10
示例 2:
输入: n = 5
输出: [0,1,1,2,1,2]
解释:
0 --> 0
1 --> 1
2 --> 10
3 --> 11
4 --> 100
5 --> 101
说明 :
0 <= n <= 105
func countBits(n int) []int {
}
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/w3tCBm
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路1:
思路1: 暴力计算每一个数字的每一位1的个数,时间复杂度 O(n*17),10^5 < ^17
代码
func countBits(n int) []int {
//思路1: 暴力计算每一个数字的每一位1的个数,时间复杂度 O(n*17),10^5 < ^17
//参数处理
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
}
测试

思路2:
// 要点: i&(i-1)就是比i中1少一个的数字
// 我们可以通过之前求到的i中1的个数来求,时间复杂度是 O(n)
代码2
func countBits(n int) []int {
// 要点: i&(i-1)就是比i中1少一个的数字
// 我们可以通过之前求到的i中1的个数来求,时间复杂度是 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
}
测试2

边栏推荐
- 基本Dos命令
- Database table splitting strategy
- Lock wait timeout exceeded try restarting transaction
- Unity3d makes the registration login interface and realizes the scene jump
- It has been solved by personal practice: MySQL row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT
- PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
- NRF24L01故障排查
- 地球围绕太阳转
- 单片机蓝牙无线烧录
- (4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
猜你喜欢

编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)

2021.11.10 compilation examination

Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance

Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)

微信小程序开发心得

基本Dos命令

使用rtknavi进行RT-PPP测试

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

Combination of fairygui check box and progress bar
随机推荐
InnoDB dirty page refresh mechanism checkpoint in MySQL
Unity3d, Alibaba cloud server, platform configuration
[Chongqing Guangdong education] Shandong University College Physics reference materials
基本Dos命令
Detailed explanation of truncate usage
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
Introduction to the daily practice column of the Blue Bridge Cup
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
There is no red exclamation mark after SVN update
[offer78] merge multiple ordered linked lists
服务未正常关闭导致端口被占用
FGUI工程打包发布&导入Unity&将UI显示出来的方式
单片机蓝牙无线烧录
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
Prove the time complexity of heap sorting
[leetcode622] design circular queue
HCIP Day 12
(课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
idea问题记录
Unity场景跳转及退出